Skip to content

Commit

Permalink
feat: add ability to translate the entire message (fixes #11)
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Jul 18, 2018
1 parent 4c9d359 commit 829b475
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,68 @@ Refer to the [Usage documentation](#usage) for common usage examples.

### `child`

The `child` function has two signatures:

1. Accepts an object.
2. Accepts a function.

#### Object input

Creates a child logger appending the provided `context` object to the previous logger context.

```js
type ChildType = (context: MessageContextType) => LoggerType;

```

Example:

```js
import log from 'roarr';

const childLog = log.child({
foo: 'bar'
});

log.debug('foo 1');
childLog.debug('foo 2');

// {"context":{"logLevel":20},"message":"foo 1","sequence":0,"time":1531914529921,"version":"1.0.0"}
// {"context":{"foo":"bar","logLevel":20},"message":"foo 2","sequence":1,"time":1531914529922,"version":"1.0.0"}

```

#### Function input

Creates a child logger where every message is intercepted.

```js
type ChildType = (context: TranslatorType) => LoggerType;

```

Example:

```js
import log from 'roarr';

const childLog = log.child((message) => {
return {
...message,
message: message.message.replace('foo', 'bar')
}
});

log.debug('foo 1');
childLog.debug('foo 2');

// {"context":{"logLevel":20},"message":"foo 1","sequence":0,"time":1531914656076,"version":"1.0.0"}
// {"context":{"logLevel":20},"message":"bar 2","sequence":1,"time":1531914656077,"version":"1.0.0"}

```

Example:

### `trace`
### `debug`
### `info`
Expand Down
14 changes: 12 additions & 2 deletions src/factories/createLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
import type {
LoggerType,
MessageContextType,
MessageType
MessageType,
TranslatorType
} from '../types';

type OnMessageEventHandlerType = (message: MessageType) => void;
Expand Down Expand Up @@ -58,7 +59,16 @@ const createLogger = (onMessage: OnMessageEventHandlerType, parentContext: Messa
});
};

log.child = (context: MessageContextType) => {
log.child = (context: TranslatorType | MessageContextType) => {
if (typeof context === 'function') {
return createLogger((message) => {
if (typeof context !== 'function') {
throw new Error('Unexpected state.');
}
onMessage(context(message))
}, parentContext);
}

return createLogger(onMessage, {
...parentContext,
...context
Expand Down
4 changes: 4 additions & 0 deletions src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import {
import {
ROARR_LOG
} from './config';
export type {
MessageType,
TranslatorType
} from './types';

global.ROARR = createRoarrInititialGlobalState(global.ROARR || {});

Expand Down
2 changes: 2 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export type MessageType = {|
+version: string
|};

export type TranslatorType = (message: MessageType) => MessageType;

declare function Logger (
context: MessageContextType,
message: string,
Expand Down
23 changes: 23 additions & 0 deletions test/roarr/roarr.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,29 @@ test('appends context to the previous child context (overrides)', (t) => {
]);
});

test('translates child message', (t) => {
const log = createLoggerWithHistory();

log
.child((message) => {
return {
...message,
message: message.message + 'bar'
}
})
('foo');

t.deepEqual(log.messages, [
{
context: {},
message: 'foobar',
sequence,
time,
version
}
]);
});

test('convenience methods trace, debug, info, warn, error and fatal prepend a logLevel property', (t) => {
const log = createLoggerWithHistory();

Expand Down

0 comments on commit 829b475

Please sign in to comment.