Skip to content

Commit d07bba2

Browse files
author
Zachary Haber
committed
chore: prettier everything
1 parent a367747 commit d07bba2

File tree

4 files changed

+75
-53
lines changed

4 files changed

+75
-53
lines changed

README.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,49 @@ npm install log4js @log4js-node/logfaces-http
88

99
## Configuration
1010

11-
* `type` - `@log4js-node/logfaces-http`
12-
* `url` - `string` - logFaces receiver servlet URL
13-
* `application` - `string` (optional, defaults to empty string) - used to identify your application's logs
14-
* `timeout` - `integer` (optional, defaults to 5000ms) - the timeout for the HTTP request.
15-
* `configContext` - function (optional) returning a global context object accessible to all appenders. Properties from configContext added as `p_` values in the logFaces event.
16-
* `hostname` - `string` (optional) - used to add the hostname `h` property to the logFaces event.
11+
- `type` - `@log4js-node/logfaces-http`
12+
- `url` - `string` - logFaces receiver servlet URL
13+
- `application` - `string` (optional, defaults to empty string) - used to identify your application's logs
14+
- `timeout` - `integer` (optional, defaults to 5000ms) - the timeout for the HTTP request.
15+
- `configContext` - function (optional) returning a global context object accessible to all appenders. Properties from configContext added as `p_` values in the logFaces event.
16+
- `hostname` - `string` (optional) - used to add the hostname `h` property to the logFaces event.
1717

1818
This appender will also pick up Logger context values from the events, and add them as `p_` values in the logFaces event. See the example below for more details. Note that Logger context may override the same properties defined in `configContext`.
1919

2020
# Example (default config)
2121

2222
```javascript
23-
2423
// global context variables can be specified like this
2524
// these variables will propagate to logFaces server with all requests
2625
const MDC = {
27-
sessionID: 111
26+
sessionID: 111,
2827
};
2928

3029
// log4js framework configuration
3130
log4js.configure({
3231
appenders: {
33-
logfaces: { type: '@log4js-node/logfaces-http', url: 'http://lfs-server/logs', application: 'TesterApp', configContext: () => MDC }
32+
logfaces: {
33+
type: "@log4js-node/logfaces-http",
34+
url: "http://lfs-server/logs",
35+
application: "TesterApp",
36+
configContext: () => MDC,
37+
},
3438
},
3539
categories: {
36-
default: { appenders: [ 'logfaces' ], level: 'info', enableCallStack: true }
37-
}
40+
default: { appenders: ["logfaces"], level: "info", enableCallStack: true },
41+
},
3842
});
3943

4044
// instances of the logger is obtained from framework like this
4145
const logger = log4js.getLogger();
4246

4347
// local context variables can propagate to logFaces along with global context
44-
logger.addContext('requestId', '123');
45-
logger.info('some interesting log message');
48+
logger.addContext("requestId", "123");
49+
logger.info("some interesting log message");
4650

4751
// global context variables can be modified anywhere in the app
4852
MDC.sessionID = 222;
49-
logger.error('something has gone wrong', new Error('exception message'));
53+
logger.error("something has gone wrong", new Error("exception message"));
5054
```
55+
5156
This example will result in two log events being sent to `lfs-server`. Both events will have a `requestId` property with a value of `123`. First event will have `sessionID` of `111` and second `sessionID` of `222`. Also since `enableCallStack` is set, both events will include location details such as file name, function name and line number. Second event will have a stack trace of a trown error.

lib/index.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ const axios = require('axios');
1010

1111
function format(logData) {
1212
// do not format error with stack, it is reported separetely
13-
const filtered = logData.filter((item) => !(item instanceof Error && item.stack));
13+
const filtered = logData.filter(
14+
(item) => !(item instanceof Error && item.stack)
15+
);
1416
return util.format.apply(util, filtered);
1517
}
1618

@@ -36,7 +38,7 @@ function logFacesAppender(config) {
3638
baseURL: config.url,
3739
timeout: config.timeout || 5000,
3840
headers: { 'Content-Type': 'application/json' },
39-
withCredentials: true
41+
withCredentials: true,
4042
});
4143

4244
const configContext = config.configContext;
@@ -49,7 +51,7 @@ function logFacesAppender(config) {
4951
p: event.level.levelStr, // level (priority)
5052
g: event.categoryName, // logger name
5153
m: format(event.data), // message text
52-
h: config.hostname // hostname
54+
h: config.hostname, // hostname
5355
};
5456

5557
if (event.fileName) {
@@ -82,14 +84,17 @@ function logFacesAppender(config) {
8284
});
8385

8486
// send to server
85-
sender.post('', lfsEvent)
86-
.catch((error) => {
87-
if (error.response) {
88-
console.error(`log4js.logFaces-HTTP Appender error posting to ${config.url}: ${error.response.status} - ${error.response.data}`); //eslint-disable-line
89-
} else {
90-
console.error(`log4js.logFaces-HTTP Appender error: ${error.message}`); //eslint-disable-line
91-
}
92-
});
87+
sender.post('', lfsEvent).catch((error) => {
88+
if (error.response) {
89+
// eslint-disable-next-line
90+
console.error(
91+
`log4js.logFaces-HTTP Appender error posting to ${config.url}: ${error.response.status} - ${error.response.data}`
92+
);
93+
} else {
94+
// eslint-disable-next-line
95+
console.error(`log4js.logFaces-HTTP Appender error: ${error.message}`);
96+
}
97+
});
9398
};
9499
}
95100

test/sandbox-coverage.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ const NYC = require('nyc');
55

66
sandbox.configure({
77
sourceTransformers: {
8-
nyc: function (source) {
8+
nyc(source) {
99
if (this.filename.indexOf('node_modules') > -1) {
1010
return source;
1111
}
1212
const nyc = new NYC({});
13-
return nyc.instrumenter().instrumentSync(source, this.filename, { registerMap: () => {} });
14-
}
15-
}
13+
return nyc
14+
.instrumenter()
15+
.instrumentSync(source, this.filename, { registerMap: () => {} });
16+
},
17+
},
1618
});

test/tap/index-test.js

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,55 @@ const appender = require('../../lib');
66

77
function setupLogging(category, enableCallStack, options) {
88
const fakeAxios = {
9-
create: function (config) {
9+
create(config) {
1010
this.config = config;
1111
return {
12-
post: function (emptyString, event) {
12+
post(emptyString, event) {
1313
fakeAxios.args = [emptyString, event];
1414
return {
15-
catch: function (cb) {
15+
catch(cb) {
1616
fakeAxios.errorCb = cb;
17-
}
17+
},
1818
};
19-
}
19+
},
2020
};
21-
}
21+
},
2222
};
2323

2424
const fakeConsole = {
25-
error: function (msg) {
25+
error(msg) {
2626
this.msg = msg;
27-
}
27+
},
2828
};
2929

3030
const appenderModule = sandbox.require('../../lib', {
3131
requires: {
32-
axios: fakeAxios
32+
axios: fakeAxios,
3333
},
3434
globals: {
35-
console: fakeConsole
36-
}
35+
console: fakeConsole,
36+
},
3737
});
3838
const log4js = sandbox.require('log4js', {
3939
requires: {
40-
'@log4js-node/logfaces-http': appenderModule
40+
'@log4js-node/logfaces-http': appenderModule,
4141
},
42-
ignoreMissing: true
42+
ignoreMissing: true,
4343
});
4444

4545
options.type = '@log4js-node/logfaces-http';
4646
log4js.configure({
4747
appenders: { http: options },
48-
categories: { default: { appenders: ['http'], level: 'trace', enableCallStack: enableCallStack } }
48+
categories: {
49+
default: { appenders: ['http'], level: 'trace', enableCallStack },
50+
},
4951
});
5052

5153
return {
5254
logger: log4js.getLogger(category),
5355
logger2: log4js.getLogger(category),
54-
fakeAxios: fakeAxios,
55-
fakeConsole: fakeConsole
56+
fakeAxios,
57+
fakeConsole,
5658
};
5759
}
5860

@@ -66,14 +68,19 @@ test('logFaces appender', (batch) => {
6668
const setup = setupLogging('myCategory', false, {
6769
application: 'LFS-HTTP',
6870
url: 'http://localhost/receivers/rx1',
69-
hostname: 'localhost'
71+
hostname: 'localhost',
7072
});
7173

7274
t.test('axios should be configured', (assert) => {
73-
assert.equal(setup.fakeAxios.config.baseURL, 'http://localhost/receivers/rx1');
75+
assert.equal(
76+
setup.fakeAxios.config.baseURL,
77+
'http://localhost/receivers/rx1'
78+
);
7479
assert.equal(setup.fakeAxios.config.timeout, 5000);
7580
assert.equal(setup.fakeAxios.config.withCredentials, true);
76-
assert.same(setup.fakeAxios.config.headers, { 'Content-Type': 'application/json' });
81+
assert.same(setup.fakeAxios.config.headers, {
82+
'Content-Type': 'application/json',
83+
});
7784
assert.end();
7885
});
7986

@@ -107,15 +114,18 @@ test('logFaces appender', (batch) => {
107114
'log4js.logFaces-HTTP Appender error posting to http://localhost/receivers/rx1: 500 - oh no'
108115
);
109116
setup.fakeAxios.errorCb(new Error('oh dear'));
110-
assert.equal(setup.fakeConsole.msg, 'log4js.logFaces-HTTP Appender error: oh dear');
117+
assert.equal(
118+
setup.fakeConsole.msg,
119+
'log4js.logFaces-HTTP Appender error: oh dear'
120+
);
111121
assert.end();
112122
});
113123
t.end();
114124
});
115125

116126
batch.test('should serialise stack traces correctly', (t) => {
117127
const setup = setupLogging('stack-traces', false, {
118-
url: 'http://localhost/receivers/rx1'
128+
url: 'http://localhost/receivers/rx1',
119129
});
120130

121131
setup.logger.error('Oh no', new Error('something went wrong'));
@@ -131,7 +141,7 @@ test('logFaces appender', (batch) => {
131141
batch.test('log event should contain locations', (t) => {
132142
const setup = setupLogging('myCategory', true, {
133143
application: 'LFS-HTTP',
134-
url: 'http://localhost/receivers/rx1'
144+
url: 'http://localhost/receivers/rx1',
135145
});
136146

137147
setup.logger.info('Log event #1');
@@ -150,13 +160,13 @@ test('logFaces appender', (batch) => {
150160

151161
batch.test('can handle global context', (t) => {
152162
const ctx = {
153-
sessionID: 111
163+
sessionID: 111,
154164
};
155165

156166
const setup = setupLogging('myCategory', false, {
157167
application: 'LFS-HTTP',
158168
url: 'http://localhost/receivers/rx1',
159-
configContext: () => ctx
169+
configContext: () => ctx,
160170
});
161171

162172
t.test('event has properties from config context', (assert) => {

0 commit comments

Comments
 (0)