Skip to content

Commit

Permalink
feat: remove global.ROARR.prepend
Browse files Browse the repository at this point in the history
This is technically a breaking change – if you had global.ROARR.prepend configured, it will no longer be appended to your logs. I have never had practical use case to this. Turns out that combination of instance ID and sequence ID is enough to create a logical timeline of the logs.
  • Loading branch information
gajus committed May 11, 2018
1 parent 09a6115 commit dffac37
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 137 deletions.
110 changes: 1 addition & 109 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ JSON logger for Node.js and browser.

* [Motivation](#motivation)
* [Usage](#usage)
* [Prepending context using the global state](#prepending-context-using-the-global-state)
* [Filtering logs](#filtering-logs)
* [jq primer](#jq-primer)
* [Log message format](#log-message-format)
Expand Down Expand Up @@ -48,7 +47,7 @@ I needed a logger that:
* [Decouples transports](#transports).
* Has a [CLI program](#cli-program).
* Works in Node.js and browser.
* Configurable using environment variables and [`global`](https://nodejs.org/api/globals.html) namespace.
* Configurable using environment variables.

In other words,

Expand Down Expand Up @@ -96,77 +95,6 @@ Produces output:
```

### Prepending context using the global state

Prepending context using the global state will affect all `roarr` logs.

```js
import log from 'roarr';

log('foo');

global.ROARR.prepend = {
taskId: 1
};

log('bar');

global.ROARR.prepend = {};

log('baz');

```

Produces output:

```
{"context":{},"message":"foo","sequence":0,"time":1506776210000,"version":"1.0.0"}
{"context":{"taskId":1},"message":"bar","sequence":1,"time":1506776210000,"version":"1.0.0"}
{"context":{},"message":"baz","sequence":2,"time":1506776210000,"version":"1.0.0"}
```

Prepending context using the global state is useful when the desired result is to associate all logs with a specific context for a duration of an operation, e.g. to correlate the main process logs with the dependency logs.

```js
import log from 'roarr';
import foo from 'foo';

const taskIds = [
1,
2,
3
];

for (const taskId of taskIds) {
global.ROARR = global.ROARR || {};
global.ROARR.prepend = {
taskId
};

log('starting task ID %d', taskId);

// In this example, `foo` is an arbitrary third-party dependency that is using
// roarr logger.
foo(taskId);

log('successfully completed task ID %d', taskId);

global.ROARR.prepend = {};
}

```

Produces output:

```
{"context":{"taskId":1},"message":"starting task ID 1","sequence":0,"time":1506776210000,"version":"1.0.0"}
{"context":{"taskId":1},"message":"foo","sequence":1,"time":1506776210000,"version":"1.0.0"}
{"context":{"taskId":1},"message":"successfully completed task ID 1","sequence":2,"time":1506776210000,"version":"1.0.0"}
[...]
```

### Filtering logs

Roarr is designed to print all or none logs (refer to the [`ROARR_LOG` environment variable](#environment-variables) documentation).
Expand Down Expand Up @@ -424,25 +352,6 @@ I recommend to create a file `Logger.js` in the project directory. Use this file
*/

import log from 'roarr';
import ulid from 'ulid';

// Instance ID is useful for correlating logs in high concurrency environment.
//
// See `roarr augment --append-instance-id` option as an alternative way to
// append instance ID to all logs.
const instanceId = ulid();

// The reason we are using `global.ROARR.prepend` as opposed to `roarr#child`
// is because we want this information to be prepended to all logs, including
// those of the "my-application" dependencies.
//
// Note: If you are adding logger to a package intended to be consumed by other
// packages, you must not set `global.ROARR.prepend`. Instead, use `roarr#child`.
global.ROARR.prepend = {
...global.ROARR.prepend,
application: 'my-application',
instanceId
};

const Logger = log.child({
// .foo property is going to appear only in the logs that are created using
Expand All @@ -454,23 +363,6 @@ export default Logger;

```

### Using Roarr in modules

If you are developing a code that is designed to be consumed by other applications/ modules, then you should avoid using global.ROARR (though, there are valid use cases). However, you should still start the project by defining a `Logger.js` file and use log.child instead.

```js
/**
* @file Example contents of a Logger.js file.
*/
import Roarr from 'roarr';

export default Roarr.child({
domain: 'database',
package: 'my-package'
});

```

Roarr does not have reserved context property names. However, I encourage use of the conventions. The `roarr pretty-print` [CLI program](#cli-program) is using the context property names suggested in the [conventions](#conventions) to pretty-print the logs for the developer inspection purposes.

## Recipes
Expand Down
2 changes: 0 additions & 2 deletions src/factories/createLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const createLogger = (onMessage: OnMessageEventHandlerType, parentContext: Messa

if (typeof a === 'string') {
context = {
...global.ROARR.prepend,
...parentContext
};
message = sprintf(a, b, c, d, e, f, g, h, i, k);
Expand All @@ -43,7 +42,6 @@ const createLogger = (onMessage: OnMessageEventHandlerType, parentContext: Messa
}

context = {
...global.ROARR.prepend,
...parentContext,
...a
};
Expand Down
26 changes: 0 additions & 26 deletions test/roarr/roarr.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,32 +195,6 @@ test('appends context to the previous child context (overrides)', (t) => {
]);
});

test('prepends global.ROARR.prepend context', (t) => {
const log = createLoggerWithHistory();

global.ROARR.prepend = {
bar: 'BAR 0',
foo: 'FOO'
};

log({
bar: 'BAR 1'
}, 'baz');

t.deepEqual(log.messages, [
{
context: {
bar: 'BAR 1',
foo: 'FOO'
},
message: 'baz',
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 dffac37

Please sign in to comment.