Skip to content

Commit 8084e80

Browse files
author
Gareth Jones
committed
fix(tests): tests failing - config listeners not working in sandbox
1 parent da703cd commit 8084e80

17 files changed

Lines changed: 1013 additions & 930 deletions

examples/layouts.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const log4js = require('../lib/log4js');
2+
3+
log4js.configure({
4+
appenders: {
5+
out: { type: 'stdout', layout: { type: 'messagePassThrough' } }
6+
},
7+
categories: {
8+
default: { appenders: ['out'], level: 'info' }
9+
}
10+
});
11+
12+
const logger = log4js.getLogger('thing');
13+
logger.info('This should not have a timestamp');

lib/LoggingEvent.js

Lines changed: 70 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,80 @@
11
const CircularJSON = require('circular-json');
2+
const levels = require('./levels');
23

3-
module.exports = (levels) => {
4+
/**
5+
* @name LoggingEvent
6+
* @namespace Log4js
7+
*/
8+
class LoggingEvent {
49
/**
5-
* @name LoggingEvent
6-
* @namespace Log4js
10+
* Models a logging event.
11+
* @constructor
12+
* @param {String} categoryName name of category
13+
* @param {Log4js.Level} level level of message
14+
* @param {Array} data objects to log
15+
* @author Seth Chisamore
716
*/
8-
class LoggingEvent {
9-
/**
10-
* Models a logging event.
11-
* @constructor
12-
* @param {String} categoryName name of category
13-
* @param {Log4js.Level} level level of message
14-
* @param {Array} data objects to log
15-
* @author Seth Chisamore
16-
*/
17-
constructor(categoryName, level, data, context) {
18-
this.startTime = new Date();
19-
this.categoryName = categoryName;
20-
this.data = data;
21-
this.level = level;
22-
this.context = Object.assign({}, context);
23-
this.pid = process.pid;
24-
// if (cluster && cluster.isWorker) {
25-
// this.cluster = {
26-
// workerId: cluster.worker.id,
27-
// worker: process.pid
28-
// };
29-
// }
30-
}
17+
constructor(categoryName, level, data, context) {
18+
this.startTime = new Date();
19+
this.categoryName = categoryName;
20+
this.data = data;
21+
this.level = level;
22+
this.context = Object.assign({}, context);
23+
this.pid = process.pid;
24+
// if (cluster && cluster.isWorker) {
25+
// this.cluster = {
26+
// workerId: cluster.worker.id,
27+
// worker: process.pid
28+
// };
29+
// }
30+
}
3131

32-
serialise() {
33-
// JSON.stringify(new Error('test')) returns {}, which is not really useful for us.
34-
// The following allows us to serialize errors correctly.
35-
// Validate that we really are in this case
36-
try {
37-
const logData = this.data.map((e) => {
38-
if (e && e.stack && CircularJSON.stringify(e) === '{}') {
39-
e = { message: e.message, stack: e.stack };
40-
}
41-
return e;
42-
});
43-
this.data = logData;
44-
return CircularJSON.stringify(this);
45-
} catch (e) {
46-
return new LoggingEvent(
47-
'log4js',
48-
levels.ERROR,
49-
['Unable to serialise log event due to :', e]
50-
).serialise();
51-
}
32+
serialise() {
33+
// JSON.stringify(new Error('test')) returns {}, which is not really useful for us.
34+
// The following allows us to serialize errors correctly.
35+
// Validate that we really are in this case
36+
try {
37+
const logData = this.data.map((e) => {
38+
if (e && e.stack && CircularJSON.stringify(e) === '{}') {
39+
e = { message: e.message, stack: e.stack };
40+
}
41+
return e;
42+
});
43+
this.data = logData;
44+
return CircularJSON.stringify(this);
45+
} catch (e) {
46+
return new LoggingEvent(
47+
'log4js',
48+
levels.ERROR,
49+
['Unable to serialise log event due to :', e]
50+
).serialise();
5251
}
52+
}
5353

54-
static deserialise(serialised) {
55-
let event;
56-
try {
57-
event = CircularJSON.parse(serialised);
58-
event.startTime = new Date(event.startTime);
59-
event.level = levels.getLevel(event.level.levelStr);
60-
event.data = event.data.map((e) => {
61-
if (e && e.stack) {
62-
const fakeError = new Error(e.message);
63-
fakeError.stack = e.stack;
64-
e = fakeError;
65-
}
66-
return e;
67-
});
68-
} catch (e) {
69-
event = new LoggingEvent(
70-
'log4js',
71-
levels.ERROR,
72-
['Unable to parse log:', serialised, 'because: ', e]
73-
);
74-
}
75-
76-
return event;
54+
static deserialise(serialised) {
55+
let event;
56+
try {
57+
event = CircularJSON.parse(serialised);
58+
event.startTime = new Date(event.startTime);
59+
event.level = levels.getLevel(event.level.levelStr);
60+
event.data = event.data.map((e) => {
61+
if (e && e.stack) {
62+
const fakeError = new Error(e.message);
63+
fakeError.stack = e.stack;
64+
e = fakeError;
65+
}
66+
return e;
67+
});
68+
} catch (e) {
69+
event = new LoggingEvent(
70+
'log4js',
71+
levels.ERROR,
72+
['Unable to parse log:', serialised, 'because: ', e]
73+
);
7774
}
75+
76+
return event;
7877
}
78+
}
7979

80-
return LoggingEvent;
81-
};
80+
module.exports = LoggingEvent;

lib/appenders/categoryFilter.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
'use strict';
22

3+
const debug = require('debug')('log4js:categoryFilter');
4+
35
function categoryFilter(excludes, appender) {
46
if (typeof excludes === 'string') excludes = [excludes];
57
return (logEvent) => {
8+
debug(`Checking ${logEvent.categoryName} against ${excludes}`);
69
if (excludes.indexOf(logEvent.categoryName) === -1) {
10+
debug('Not excluded, sending to appender');
711
appender(logEvent);
812
}
913
};

lib/appenders/index.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
const path = require('path');
2+
const debug = require('debug')('log4js:appenders');
3+
const configuration = require('../configuration');
4+
const clustering = require('../clustering');
5+
const levels = require('../levels');
6+
const layouts = require('../layouts');
7+
8+
const appenders = new Map();
9+
10+
const tryLoading = (modulePath, config) => {
11+
debug('Loading module from ', modulePath);
12+
try {
13+
return require(modulePath); //eslint-disable-line
14+
} catch (e) {
15+
// if the module was found, and we still got an error, then raise it
16+
configuration.throwExceptionIf(
17+
config,
18+
e.code !== 'MODULE_NOT_FOUND',
19+
`appender "${modulePath}" could not be loaded (error was: ${e})`
20+
);
21+
return undefined;
22+
}
23+
};
24+
25+
const loadAppenderModule = (type, config) => tryLoading(`./${type}`, config) ||
26+
tryLoading(type, config) ||
27+
tryLoading(path.join(path.dirname(require.main.filename), type), config) ||
28+
tryLoading(path.join(process.cwd(), type), config);
29+
30+
const createAppender = (name, config) => {
31+
const appenderConfig = config.appenders[name];
32+
const appenderModule = loadAppenderModule(appenderConfig.type, config);
33+
configuration.throwExceptionIf(
34+
config,
35+
configuration.not(appenderModule),
36+
`appender "${name}" is not valid (type "${appenderConfig.type}" could not be found)`
37+
);
38+
if (appenderModule.appender) {
39+
debug(`DEPRECATION: Appender ${appenderConfig.type} exports an appender function.`);
40+
}
41+
if (appenderModule.shutdown) {
42+
debug(`DEPRECATION: Appender ${appenderConfig.type} exports a shutdown function.`);
43+
}
44+
45+
debug(`${name}: clustering.isMaster ? ${clustering.isMaster()}`);
46+
debug(`${name}: appenderModule is ${require('util').inspect(appenderModule)}`); // eslint-disable-line
47+
return clustering.onlyOnMaster(() => {
48+
debug(`calling appenderModule.configure for ${name} / ${appenderConfig.type}`);
49+
return appenderModule.configure(
50+
appenderConfig,
51+
layouts,
52+
appender => appenders.get(appender),
53+
levels
54+
);
55+
}, () => {});
56+
};
57+
58+
const setup = (config) => {
59+
appenders.clear();
60+
61+
Object.keys(config.appenders).forEach((name) => {
62+
debug(`Creating appender ${name}`);
63+
appenders.set(name, createAppender(name, config));
64+
});
65+
};
66+
67+
// setup({
68+
// appenders: {
69+
// stdout: { type: 'stdout' }
70+
// }
71+
// });
72+
73+
configuration.addListener((config) => {
74+
configuration.throwExceptionIf(
75+
config,
76+
configuration.not(configuration.anObject(config.appenders)),
77+
'must have a property "appenders" of type object.'
78+
);
79+
const appenderNames = Object.keys(config.appenders);
80+
configuration.throwExceptionIf(
81+
config,
82+
configuration.not(appenderNames.length),
83+
'must define at least one appender.'
84+
);
85+
86+
appenderNames.forEach((name) => {
87+
configuration.throwExceptionIf(
88+
config,
89+
configuration.not(config.appenders[name].type),
90+
`appender "${name}" is not valid (must be an object with property "type")`
91+
);
92+
});
93+
});
94+
95+
configuration.addListener(setup);
96+
97+
module.exports = appenders;

0 commit comments

Comments
 (0)