Skip to content

Commit 6f73b2b

Browse files
committed
feat: implement convenience methods
1 parent 6ee1226 commit 6f73b2b

File tree

3 files changed

+168
-43
lines changed

3 files changed

+168
-43
lines changed

README.md

Lines changed: 83 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ JSON logger for Node.js.
1212
* [Prepending context using the global state](#prepending-context-using-the-global-state)
1313
* [Filtering logs](#filtering-logs)
1414
* [jq primer](#jq-primer)
15+
* [API](#api)
16+
* [`child`](#child)
17+
* [`trace`](#trace)
18+
* [`debug`](#debug)
19+
* [`info`](#info)
20+
* [`warn`](#warn)
21+
* [`error`](#error)
22+
* [`fatal`](#fatal)
1523
* [Transports](#transports)
1624
* [Environment variables](#environment-variables)
1725
* [Conventions](#conventions)
@@ -26,56 +34,13 @@ ROARR_LOG=true node ./index.js
2634

2735
```
2836

29-
`roarr` package exports a function that accepts the following API:
30-
31-
```js
32-
export type LoggerType =
33-
(
34-
context: MessageContextType,
35-
message: string,
36-
c?: SprintfArgumentType,
37-
d?: SprintfArgumentType,
38-
e?: SprintfArgumentType,
39-
f?: SprintfArgumentType,
40-
g?: SprintfArgumentType,
41-
h?: SprintfArgumentType,
42-
i?: SprintfArgumentType,
43-
k?: SprintfArgumentType
44-
) => void |
45-
(
46-
message: string,
47-
b?: SprintfArgumentType,
48-
c?: SprintfArgumentType,
49-
d?: SprintfArgumentType,
50-
e?: SprintfArgumentType,
51-
f?: SprintfArgumentType,
52-
g?: SprintfArgumentType,
53-
h?: SprintfArgumentType,
54-
i?: SprintfArgumentType,
55-
k?: SprintfArgumentType
56-
) => void;
57-
58-
```
59-
60-
Put it into words:
61-
62-
1. First parameter can be either a string (message) or an object.
63-
* If first parameter is an object (context), the second parameter must be a string (message).
64-
1. Arguments after the message parameter are used to enable [printf message formatting](https://en.wikipedia.org/wiki/Printf_format_string).
65-
* Printf arguments must be of a primitive type (`string | number | boolean | null`).
66-
* There can be up to 9 printf arguments (or 8 if the first parameter is the context object).
67-
68-
<!-- -->
69-
7037
```js
7138
import log from 'roarr';
7239

7340
log('foo');
7441

7542
log('bar %s', 'baz');
7643

77-
// Creates a child logger appending the provided `context` object
78-
// to the previous logger context.
7944
const debug = log.child({
8045
level: 'debug'
8146
});
@@ -184,6 +149,81 @@ ROARR_LOG=true node ./index.js | jq 'select(.context.level == "warning" or .cont
184149
185150
```
186151

152+
## API
153+
154+
`roarr` package exports a function that accepts the following API:
155+
156+
```js
157+
export type LoggerType =
158+
(
159+
context: MessageContextType,
160+
message: string,
161+
c?: SprintfArgumentType,
162+
d?: SprintfArgumentType,
163+
e?: SprintfArgumentType,
164+
f?: SprintfArgumentType,
165+
g?: SprintfArgumentType,
166+
h?: SprintfArgumentType,
167+
i?: SprintfArgumentType,
168+
k?: SprintfArgumentType
169+
) => void |
170+
(
171+
message: string,
172+
b?: SprintfArgumentType,
173+
c?: SprintfArgumentType,
174+
d?: SprintfArgumentType,
175+
e?: SprintfArgumentType,
176+
f?: SprintfArgumentType,
177+
g?: SprintfArgumentType,
178+
h?: SprintfArgumentType,
179+
i?: SprintfArgumentType,
180+
k?: SprintfArgumentType
181+
) => void;
182+
183+
```
184+
185+
Put it into words:
186+
187+
1. First parameter can be either a string (message) or an object.
188+
* If first parameter is an object (context), the second parameter must be a string (message).
189+
1. Arguments after the message parameter are used to enable [printf message formatting](https://en.wikipedia.org/wiki/Printf_format_string).
190+
* Printf arguments must be of a primitive type (`string | number | boolean | null`).
191+
* There can be up to 9 printf arguments (or 8 if the first parameter is the context object).
192+
193+
Refer to the [Usage](#usage) documentation for common usage examples.
194+
195+
### `child`
196+
197+
Creates a child logger appending the provided `context` object to the previous logger context.
198+
199+
```js
200+
type ChildType = (context: MessageContextType) => LoggerType;
201+
202+
```
203+
204+
### `trace`
205+
### `debug`
206+
### `info`
207+
### `warn`
208+
### `error`
209+
### `fatal`
210+
211+
Convenience methods for logging a message with `logLevel` context property value set to the name of the convenience method, e.g.
212+
213+
```js
214+
import log from 'roarr';
215+
216+
log.debug('foo');
217+
218+
```
219+
220+
Produces output:
221+
222+
```
223+
{"context":{"logLevel":"debug"},"message":"foo","sequence":0,"time":1506776210001,"version":"1.0.0"}
224+
225+
```
226+
187227
## Transports
188228

189229
A transport in most logging libraries is something that runs in-process to perform some operation with the finalised log line. For example, a transport might send the log line to a standard syslog server after processing the log line and reformatting it.

src/factories/createLogger.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ type OnMessageEventHandlerType = (message: MessageType) => void;
1616

1717
const version = '1.0.0';
1818

19+
const logLevels = [
20+
'trace',
21+
'debug',
22+
'info',
23+
'warn',
24+
'error',
25+
'fatal'
26+
];
27+
1928
const createLogger = (onMessage: OnMessageEventHandlerType, parentContext: MessageContextType = {}) => {
2029
// eslint-disable-next-line id-length
2130
const log: LoggerType = (a, b, c, d, e, f, g, h, i, k) => {
@@ -57,6 +66,14 @@ const createLogger = (onMessage: OnMessageEventHandlerType, parentContext: Messa
5766
});
5867
};
5968

69+
for (const logLevel of logLevels) {
70+
log[logLevel] = (a, b, c, d, e, f, g, h, i, k) => {
71+
return log.child({
72+
logLevel
73+
})(a, b, c, d, e, f, g, h, i, k);
74+
};
75+
}
76+
6077
return log;
6178
};
6279

test/roarr/roarr.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,71 @@ test('prepends global.ROARR.prepend context', (t) => {
220220
}
221221
]);
222222
});
223+
224+
test('convenience methods trace, debug, info, warn, error and fatal prepend a logLevel property', (t) => {
225+
const log = createLoggerWithHistory();
226+
227+
log.trace('foo 0');
228+
log.debug('foo 1');
229+
log.info('foo 2');
230+
log.warn('foo 3');
231+
log.error('foo 4');
232+
log.fatal('foo 5');
233+
234+
t.deepEqual(log.messages, [
235+
{
236+
context: {
237+
logLevel: 'trace'
238+
},
239+
message: 'foo 0',
240+
sequence: 0,
241+
time,
242+
version
243+
},
244+
{
245+
context: {
246+
logLevel: 'debug'
247+
},
248+
message: 'foo 1',
249+
sequence: 1,
250+
time,
251+
version
252+
},
253+
{
254+
context: {
255+
logLevel: 'info'
256+
},
257+
message: 'foo 2',
258+
sequence: 2,
259+
time,
260+
version
261+
},
262+
{
263+
context: {
264+
logLevel: 'warn'
265+
},
266+
message: 'foo 3',
267+
sequence: 3,
268+
time,
269+
version
270+
},
271+
{
272+
context: {
273+
logLevel: 'error'
274+
},
275+
message: 'foo 4',
276+
sequence: 4,
277+
time,
278+
version
279+
},
280+
{
281+
context: {
282+
logLevel: 'fatal'
283+
},
284+
message: 'foo 5',
285+
sequence: 5,
286+
time,
287+
version
288+
}
289+
]);
290+
});

0 commit comments

Comments
 (0)