Comparing version 2.1.3 to 2.2.0
@@ -29,4 +29,3 @@ "use strict"; | ||
if (typeof a === 'string') { | ||
context = { ...global.ROARR.prepend, | ||
...parentContext | ||
context = { ...parentContext | ||
}; | ||
@@ -39,4 +38,3 @@ message = (0, _sprintfJs.sprintf)(a, b, c, d, e, f, g, h, i, k); | ||
context = { ...global.ROARR.prepend, | ||
...parentContext, | ||
context = { ...parentContext, | ||
...a | ||
@@ -43,0 +41,0 @@ }; |
@@ -77,3 +77,3 @@ { | ||
}, | ||
"version": "2.1.3" | ||
"version": "2.2.0" | ||
} |
111
README.md
@@ -13,3 +13,2 @@ # Roarr | ||
* [Usage](#usage) | ||
* [Prepending context using the global state](#prepending-context-using-the-global-state) | ||
* [Filtering logs](#filtering-logs) | ||
@@ -47,2 +46,3 @@ * [jq primer](#jq-primer) | ||
* Does not block the event cycle (=fast). | ||
* Does not require initialisation. | ||
@@ -53,3 +53,3 @@ * Produces structured data. | ||
* Works in Node.js and browser. | ||
* Configurable using environment variables and [`global`](https://nodejs.org/api/globals.html) namespace. | ||
* Configurable using environment variables. | ||
@@ -102,73 +102,2 @@ In other words, | ||
### 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 | ||
@@ -431,22 +360,3 @@ | ||
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({ | ||
@@ -462,19 +372,2 @@ // .foo property is going to appear only in the logs that are created using | ||
### 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. | ||
@@ -481,0 +374,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
66805
396
453