8000 fix(levels): added level configuration, fixed smtp appender · LikeABossProgrammer/log4js-node@4333261 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4333261

Browse files
author
Gareth Jones
committed
fix(levels): added level configuration, fixed smtp appender
1 parent 4637831 commit 4333261

20 files changed

+704
-600
lines changed

lib/appenders/clustered.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function deserializeLoggingEvent(loggingEventString) {
3939
try {
4040
loggingEvent = JSON.parse(loggingEventString);
4141
loggingEvent.startTime = new Date(loggingEvent.startTime);
42-
loggingEvent.level = log4js.levels.toLevel(loggingEvent.level.levelStr);
42+
loggingEvent.level = log4js.levels.getLevel(loggingEvent.level.levelStr);
4343
// Unwrap serialized errors
4444
for (let i = 0; i < loggingEvent.data.length; i++) {
4545
const item = loggingEvent.data[i];

lib/appenders/gelf.js

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const zlib = require('zlib');
4-
const levels = require('../levels');
4+
// const levels = require('../levels');
55
const dgram = require('dgram');
66
const util = require('util');
77
const OS = require('os');
@@ -17,40 +17,31 @@ const LOG_NOTICE = 5; // normal, but significant, condition(unused)
1717
const LOG_INFO = 6; // informational message
1818
const LOG_DEBUG = 7; // debug-level message
1919

20-
const levelMapping = {};
21-
levelMapping[levels.ALL] = LOG_DEBUG;
22-
levelMapping[levels.TRACE] = LOG_DEBUG;
23-
levelMapping[levels.DEBUG] = LOG_DEBUG;
24-
levelMapping[levels.INFO] = LOG_INFO;
25-
levelMapping[levels.WARN] = LOG_WARNING;
26-
levelMapping[levels.ERROR] = LOG_ERROR;
27-
levelMapping[levels.FATAL] = LOG_CRIT;
28-
2920
/**
3021
* GELF appender that supports sending UDP packets to a GELF compatible server such as Graylog
3122
*
3223
* @param layout a function that takes a logevent and returns a string (defaults to none).
33-
* @param host - host to which to send logs (default:localhost)
34-
* @param port - port at which to send logs to (default:12201)
35-
* @param hostname - hostname of the current host (default:OS hostname)
36-
* @param facility - facility to log to (default:nodejs-server)
24+
* @param config.host - host to which to send logs (default:localhost)
25+
* @param config.port - port at which to send logs to (default:12201)
26+
* @param config.hostname - hostname of the current host (default:OS hostname)
27+
* @param config.facility - facility to log to (default:nodejs-server)
3728
*/
3829
/* eslint no-underscore-dangle:0 */
39-
function gelfAppender(layout, host, port, hostname, facility) {
40-
let config;
41-
let customFields;
42-
if (typeof host === 'object') {
43-
config = host;
44-
host = config.host;
45-
port = config.port;
46-
hostname = config.hostname;
47-
facility = config.facility;
48-
customFields = config.customFields;
49-
}
50-
51-
host = host || 'localhost';
52-
port = port || 12201;
53-
hostname = hostname || OS.hostname();
30+
function gelfAppender(layout, config, levels) {
31+
const levelMapping = {};
32+
levelMapping[levels.ALL] = LOG_DEBUG;
33+
levelMapping[levels.TRACE] = LOG_DEBUG;
34+
levelMapping[levels.DEBUG] = LOG_DEBUG;
35+
levelMapping[levels.INFO] = LOG_INFO;
36+
levelMapping[levels.WARN] = LOG_WARNING;
37+
levelMapping[levels.ERROR] = LOG_ERROR;
38+
levelMapping[levels.FATAL] = LOG_CRIT;
39+
40+
const host = config.host || 'localhost';
41+
const port = config.port || 12201;
42+
const hostname = config.hostname || OS.hostname();
43+
const facility = config.facility;
44+
const customFields = config.customFields;
5445

5546
const defaultCustomFields = customFields || {};
5647

@@ -142,12 +133,12 @@ function gelfAppender(layout, host, port, hostname, facility) {
142133
return app;
143134
}
144135

145-
function configure(config, layouts) {
136+
function configure(config, layouts, findAppender, levels) {
146137
let layout = layouts.messagePassThroughLayout;
147138
if (config.layout) {
148139
layout = layouts.layout(config.layout.type, config.layout);
149140
}
150-
return gelfAppender(layout, config);
141+
return gelfAppender(layout, config, levels);
151142
}
152143

153144
module.exports.configure = configure;

lib/appenders/logLevelFilter.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
'use strict';
22

3-
const levels = require('../levels');
4-
5-
function logLevelFilter(minLevelString, maxLevelString, appender) {
6-
const minLevel = levels.toLevel(minLevelString);
7-
const maxLevel = levels.toLevel(maxLevelString, levels.FATAL);
3+
function logLevelFilter(minLevelString, maxLevelString, appender, levels) {
4+
const minLevel = levels.getLevel(minLevelString);
5+
const maxLevel = levels.getLevel(maxLevelString, levels.FATAL);
86
return (logEvent) => {
97
const eventLevel = logEvent.level;
108
if (eventLevel.isGreaterThanOrEqualTo(minLevel) && eventLevel.isLessThanOrEqualTo(maxLevel)) {
@@ -13,9 +11,9 @@ function logLevelFilter(minLevelString, maxLevelString, appender) {
1311
};
1412
}
1513

16-
function configure(config, layouts, findAppender) {
14+
function configure(config, layouts, findAppender, levels) {
1715
const appender = findAppender(config.appender);
18-
return logLevelFilter(config.level, config.maxLevel, appender);
16+
return logLevelFilter(config.level, config.maxLevel, appender, levels);
1917
}
2018

2119
module.exports.configure = configure;

lib/appenders/multiprocess.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const debug = require('debug')('log4js:multiprocess');
4-
const levels = require('../levels');
54
const net = require('net');
65

76
const END_MSG = '__LOG4JS__';
@@ -11,7 +10,7 @@ const END_MSG = '__LOG4JS__';
1110
* Output goes to config.actualAppender (config.appender is used to
1211
* set up that appender).
1312
*/
14-
function logServer(config, actualAppender) {
13+
function logServer(config, actualAppender, levels) {
1514
/**
1615
* Takes a utf-8 string, returns an object with
1716
* the correct log properties.
@@ -22,7 +21,7 @@ function logServer(config, actualAppender) {
2221
try {
2322
loggingEvent = JSON.parse(msg);
2423
loggingEvent.startTime = new Date(loggingEvent.startTime);
25-
loggingEvent.level = levels.toLevel(loggingEvent.level.levelStr);
24+
loggingEvent.level = levels.getLevel(loggingEvent.level.levelStr);
2625
} catch (e) {
2726
// JSON.parse failed, just log the contents probably a naughty.
2827
loggingEvent = {
@@ -148,17 +147,17 @@ function workerAppender(config) {
148147
return log;
149148
}
150149

151-
function createAppender(config, appender) {
150+
function createAppender(config, appender, levels) {
152151
if (config.mode === 'master') {
153152
debug('Creating master appender');
154-
return logServer(config, appender);
153+
return logServer(config, appender, levels);
155154
}
156155

157156
debug('Creating worker appender');
158157
return workerAppender(config);
159158
}
160159

161-
function configure(config, layouts, findAppender) {
160+
function configure(config, layouts, findAppender, levels) {
162161
let appender;
163162
debug(`configure with mode = ${config.mode}`);
164163
if (config.mode === 'master') {
@@ -173,7 +172,7 @@ function configure(config, layouts, findAppender) {
173172
throw new Error(`multiprocess master appender "${config.appender}" not defined`);
174173
}
175174
}
176-
return createAppender(config, appender);
175+
return createAppender(config, appender, levels);
177176
}
178177

179178
module.exports.configure = configure;

lib/appenders/slack.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
'use strict';
22

33
const Slack = require('slack-node');
4-
const layouts = require('../layouts');
5-
6-
let layout;
7-
let slack;
8-
9-
function slackAppender(_config, _layout) {
10-
layout = _layout || layouts.basicLayout;
114

5+
function slackAppender(_config, layout, slack) {
126
return (loggingEvent) => {
137
const data = {
148
channel_id: _config.channel_id,
@@ -31,16 +25,15 @@ function slackAppender(_config, _layout) {
3125
};
3226
}
3327

34-
function configure(_config) {
28+
function configure(_config, layouts) {
29+
const slack = new Slack(_config.token);
30+
31+
let layout = layouts.basicLayout;
3532
if (_config.layout) {
3633
layout = layouts.layout(_config.layout.type, _config.layout);
3734
}
3835

39-
slack = new Slack(_config.token);
40-
41-
return slackAppender(_config, layout);
36+
return slackAppender(_config, layout, slack);
4237
}
4338

44-
module.exports.name = 'slack';
45-
module.exports.appender = slackAppender;
4639
module.exports.configure = configure;

0 commit comments

Comments
 (0)
0