Skip to content

Commit

Permalink
add format config function
Browse files Browse the repository at this point in the history
  • Loading branch information
lizheming committed Nov 10, 2017
1 parent c1115c7 commit f2fb5c0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 37 deletions.
8 changes: 7 additions & 1 deletion src/adapter/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ const log4js = require('log4js');
const _logger = Symbol('_logger');

module.exports = class {
constructor() {
constructor(config/*, clusterMode */) {
this[_logger] = {};
const logConfig = this.formatConfig(config);
this.setLogger(logConfig);
}

debug(...args) {
Expand Down Expand Up @@ -36,4 +38,8 @@ module.exports = class {
this.configure(config);
this[_logger] = log4js.getLogger(category);
}

formatConfig(config) {
return config;
}
};
13 changes: 4 additions & 9 deletions src/adapter/console.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
const Base = require('./base');

module.exports = class ConsoleLogger extends Base {
constructor(config) {
super(config);

const lConfig = Object.assign({}, config);
let {level, layout} = lConfig;
formatConfig(config) {
let {level, layout} = config;
level = level ? level.toUpperCase() : 'ALL';
layout = layout || {type: 'pattern', pattern: '%[[%d] [%z] [%p]%] - %m'};

config = Object.assign({
return Object.assign({
appenders: {
console: {type: 'console', layout}
},
categories: {
default: {appenders: ['console'], level}
}
}, lConfig);

this.setLogger(config);
}, config);
}
};
14 changes: 4 additions & 10 deletions src/adapter/datefile.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
const Base = require('./base');

module.exports = class DateFileLogger extends Base {
constructor(config, clusterMode) {
super(config);

const lConfig = Object.assign({}, config);
formatConfig(config) {
// eslint-disable-next-line prefer-const
let {level, filename, pattern, alwaysIncludePattern, absolute, layout} = lConfig;
let {level, filename, pattern, alwaysIncludePattern, absolute, layout} = config;
level = level ? level.toUpperCase() : 'ALL';

// combine config for date file appender, common config for log4js
config = Object.assign({
return Object.assign({
appenders: {
dateFile: {type: 'dateFile', filename, pattern, alwaysIncludePattern, absolute, layout}
},
categories: {
default: {appenders: ['dateFile'], level}
}
}, lConfig);

this.setLogger(config);
}, config);
}
};
11 changes: 3 additions & 8 deletions src/adapter/file.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
const Base = require('./base');

module.exports = class FileLogger extends Base {
constructor(config, clusterMode) {
super(config);

const lConfig = Object.assign({}, config);
formatConfig(config) {
// eslint-disable-next-line prefer-const
let {level, filename, maxLogSize, backups, absolute, layout} = config;
level = level ? level.toUpperCase() : 'ALL';

// combine config for file appender, common config for log4js
config = Object.assign({
return Object.assign({
appenders: {
file: {type: 'file', filename, maxLogSize, backups, absolute, layout}
},
categories: {
default: {appenders: ['file'], level}
}
}, lConfig);

this.setLogger(config);
}, config);
}
};
13 changes: 4 additions & 9 deletions src/adapter/gelf.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
const Base = require('./base');

module.exports = class GelfLogger extends Base {
constructor(config) {
super(config);

const lConfig = Object.assign({}, config);
const {level, host, hostname, port, facility} = lConfig;
config = Object.assign({
formatConfig(config) {
const {level, host, hostname, port, facility} = config;
return Object.assign({
appenders: {
gelf: {type: 'gelf', host, hostname, port, facility}
},
categories: {
default: {appenders: ['gelf'], level}
}
}, lConfig);

this.setLogger(config);
}, config);
}
};

0 comments on commit f2fb5c0

Please sign in to comment.