Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update:升级log4js,升级大版本 #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/node_modules/*
!**/*.js
89 changes: 89 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Created by wangkm on 2019-3-15.
* Email: [email protected].
*/
module.exports = {
'env': {
'es6': true,
'node': true,
'browser': true,
'commonjs': true,
},
'extends': 'eslint:recommended',
'parserOptions': {
'ecmaVersion': 2018,
'sourceType': 'module'
},
'root': true, //以当前目录为根目录
'globals': {
'__stack': true, //global
},
'rules': {
'no-console': 0, //允许使用console
'indent': [
'error',
2,
{
'SwitchCase': 1
}
],
'linebreak-style': [
'error',
'unix'
],
'quotes': [
'error',
'single'
],
'semi': [
'error',
'always'
],
'brace-style': 'error', //大括号风格
'block-spacing': 'error', //空格
'for-direction': 'error', //禁止 for 循环出现方向错误的循环,比如 for (i = 0; i < 10; i--)
'no-await-in-loop': 0, //禁止将 await 写在循环里,因为这样就无法同时发送多个异步请求了(@off 要求太严格了,有时需要在循环中写 await)
'no-class-assign': 2, //禁止给类赋值
'no-cond-assign': 2, //禁止在条件表达式中使用赋值语句
'no-const-assign': 2, //禁止修改const声明的变量
'no-constant-condition': 0, //@off 禁止在条件中使用常量表达式 if(true) if(1)
'no-dupe-keys': 2, //在创建对象字面量时不允许键重复 {a:1,a:1}
'no-dupe-args': 2, //函数参数不能重复
'no-duplicate-case': 2, //switch中的case标签不能重复
'no-empty': 2, //块语句中的内容不能为空
'no-empty-character-class': 2, //正则表达式中的[]内容不能为空
'no-func-assign': 2, //禁止重复的函数声明
'no-irregular-whitespace': 2, //不能有不规则的空格
'no-loop-func': 1, //禁止在循环中使用函数(如果没有引用外部变量不形成闭包就可以)
'no-multi-spaces': 1, //不能用多余的空格
'no-mixed-spaces-and-tabs': [2, false], //禁止混用tab和空格
'no-multiple-empty-lines': [1, {'max': 2}], //空行最多不能超过2行
'no-spaced-func': 2, //函数调用时 函数名与()之间不能有空格
'no-trailing-spaces': 1, //一行结束后面不要有空格
'no-this-before-super': 0, //在调用super()之前不能使用this或super
'no-throw-literal': 2, //禁止抛出字面量错误 throw "error";
'no-undef': 2, //不能有未定义的变量
'no-unused-vars': 1, //该规则旨在消除未使用的变量,函数和函数的参数
'no-var': 0, //禁用var,用let和const代替
'no-with': 2, //禁用with
'array-bracket-spacing': [2, 'never'], //是否允许非空数组里面有多余的空格
'arrow-parens': 0, //箭头函数用小括号括起来
'arrow-spacing': 0, //=>的前/后括号
'accessor-pairs': 0, //在对象中使用getter/setter
'camelcase': 1, //驼峰法命名
'comma-style': [2, 'last'], //逗号风格,换行时在行首还是行尾
'complexity': [0, 15], //循环复杂度brace-style
'curly': [2, 'all'], //必须使用 if(){} 中的{}
'default-case': 2, //switch语句最后必须有default
'max-depth': ['error', 4], //嵌套深度
'id-length': [1, {'min': 1, 'max': 30}], //变量名长度
'valid-jsdoc': 0, //jsdoc规则
'key-spacing': 2, //键名和键值之间要有空格。
'keyword-spacing': [2, {'before': true, 'after': true}], //此规则强制执行围绕关键字和关键字标记的一致空格
'space-before-function-paren': 0, //函数前空格
'space-before-blocks': 'error', //此规则将强化块之前的间距一致性。它只适用于不以新行开始的块
'new-cap': 1, //构造函数的名字以大写字母开始。
'comma-spacing': [2, {'before': false, 'after': true}], //,后添加空格
'space-infix-ops': ['error', {'int32Hint': false}], //这条规则旨在确保中缀操作员周围有空间。
}
};
72 changes: 63 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,63 @@
.project
logs/
config/
node_modules/*
.settings/
**/*.svn
*.svn
*.sublime-project
*.sublime-workspace
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next

.idea
19 changes: 0 additions & 19 deletions .jshintrc

This file was deleted.

97 changes: 68 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,66 @@ npm install pomelo-logger
```

## Features
### log prefix
besides category, you can output prefix as you like in your log
prefix can be filename, serverId, serverType, host etc
to use this feature, you just pass prefix params to getLogger function
```
var logger = require('pomelo-logger').getLogger(category, prefix1, prefix2, ...);
```
log output msg will output with prefix ahead

### get line number in debug
when in debug environment, you may want to get the line number of the log
to use this feature, add this code
```
process.env.LOGGER_LINE = true;
```

in pomelo, you just configure the log4js file and set **lineDebug** for true
```
### config
in pomelo, you just configure the log4js file
```json
{
"appenders": [
],

"levels": {
},

"replaceConsole": true,

"lineDebug": true
"appenders": {
"console": {
"type": "console",
"layout": {
"type": "pattern",
"pattern": "%[[%d{yyyy-MM-dd hh:mm:ss}]-[%p]-[pid=%z]-[%f{1}-%l] %m%]"
}
},
"rpc-log": {
"type": "dateFile",
"filename": "./logs/rpc-log-${opts:serverId}.log",
"alwaysIncludePattern": true,
"pattern": "yyyy-MM-dd.log",
"layout": {
"type": "pattern",
"pattern": "%[[%d{yyyy-MM-dd hh:mm:ss}]-[%p]-[pid=%z]-[%f{2}-%l] %m%]"
}
},
"crash": {
"type": "file",
"filename": "./logs/crash.log",
"maxLogSize": 31457280,
"backups": 5,
"layout": {
"type": "pattern",
"pattern": "%[[%d{yyyy-MM-dd hh:mm:ss}]-[%p]-[pid=%z]-[%f-%l] %m%]"
}
}
},
"categories": {
"default": {
"appenders": [
"console"
],
"level": "debug",
"enableCallStack": true
},
"rpc-log": {
"appenders": [
"rpc-log",
"console"
],
"level": "error",
"enableCallStack": true
},
"crash": {
"appenders": [
"crash",
"console"
],
"level": "error",
"enableCallStack": true
}
},
"rawMessage": false,
"reloadSecs": 180
}
```

Expand All @@ -53,14 +85,22 @@ in pomelo, you just configure the log4js file and set **rawMessage** for true
"appenders": [
],

"levels": {
"categories": {
},

"replaceConsole": true,

"rawMessage": true
}
```
### log prefix
besides category, you can output prefix as you like in your log
prefix can be filename, serverId, serverType, host etc
to use this feature, you just pass prefix params to getLogger function
```
var logger = require('pomelo-logger').getLogger(category, prefix1, prefix2, ...);
```
log output msg will output with prefix ahead

### dynamic configure logger level
in pomelo logger configuration file log4js.json, you can add reloadSecs option. The reloadSecs means reload logger configuration file every given time. For example
Expand All @@ -76,7 +116,6 @@ log.js
```
var logger = require('pomelo-logger').getLogger('log', __filename, process.pid);

process.env.LOGGER_LINE = true;
logger.info('test1');
logger.warn('test2');
logger.error('test3');
Expand Down
8 changes: 5 additions & 3 deletions examples/log.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var logger = require('pomelo-logger').getLogger('log', __filename, process.pid);
const log = require('../lib/logger');
log.configure(require('./log4js.json'), {serverId: 12});
let logger = log.getLogger('rpc-log', __filename);

process.env.LOGGER_LINE = true;
logger.info('test1');
logger.warn('test2');
logger.error('test3');
logger.error('test3');
log.getLogger('crash', __filename).error('one');
58 changes: 58 additions & 0 deletions examples/log4js.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"appenders": {
"console": {
"type": "console",
"layout": {
"type": "pattern",
"pattern": "%[[%d{yyyy-MM-dd hh:mm:ss}]-[%p]-[pid=%z]-[%f{1}-%l] %m%]"
}
},
"rpc-log": {
"type": "dateFile",
"filename": "./logs/rpc-log-${opts:serverId}.log",
"alwaysIncludePattern": true,
"pattern": "yyyy-MM-dd.log",
"layout": {
"type": "pattern",
"pattern": "%[[%d{yyyy-MM-dd hh:mm:ss}]-[%p]-[pid=%z]-[%f{2}-%l] %m%]"
}
},
"crash": {
"type": "file",
"filename": "./logs/crash.log",
"maxLogSize": 31457280,
"backups": 5,
"layout": {
"type": "pattern",
"pattern": "%[[%d{yyyy-MM-dd hh:mm:ss}]-[%p]-[pid=%z]-[%f-%l] %m% %s]"
}
}
},
"categories": {
"default": {
"appenders": [
"console"
],
"level": "debug",
"enableCallStack": true
},
"rpc-log": {
"appenders": [
"rpc-log",
"console"
],
"level": "error",
"enableCallStack": true
},
"crash": {
"appenders": [
"crash",
"console"
],
"level": "error",
"enableCallStack": true
}
},
"rawMessage": false,
"reloadSecs": 180
}
Loading