You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// {"name":"redis","action":"query","level":50,"time":"2016-08-15T08:50:23.569Z","error_name":"Error","error_stack":"Error: Unauthorized\n at Object.<anonymous> (/home/blacksonic/workspace/bunyan-debug/example.js:15:32)\n at Module._compile (module.js:541:32)\n at Object.Module._extensions..js (module.js:550:10)\n at Module.load (module.js:458:32)\n at tryModuleLoad (module.js:417:12)\n at Function.Module._load (module.js:409:3)\n at Module.runMain (module.js:575:10)\n at run (bootstrap_node.js:352:7)\n at startup (bootstrap_node.js:144:9)\n at bootstrap_node.js:467:3","error_message":"Unauthorized","problem":"missmatch"}
23
28
```
24
29
25
-
will output
30
+
More examples can be found in the `examples` directory.
31
+
32
+
### API
33
+
34
+
##### JsonLogger(namespace)
35
+
36
+
The default export of the library acts as a factory method.
37
+
Returns a logging instance with the given namespace.
38
+
The `DEBUG` environment variable is then used to enable these instances based on comma-delimited names.
{"name":"redis","action":"query","level":50,"time":"2016-08-15T08:50:23.569Z","error_name":"Error","error_stack":"Error: Unauthorized\n at Object.<anonymous> (/home/blacksonic/workspace/bunyan-debug/example.js:15:32)\n at Module._compile (module.js:541:32)\n at Object.Module._extensions..js (module.js:550:10)\n at Module.load (module.js:458:32)\n at tryModuleLoad (module.js:417:12)\n at Function.Module._load (module.js:409:3)\n at Module.runMain (module.js:575:10)\n at run (bootstrap_node.js:352:7)\n at startup (bootstrap_node.js:144:9)\n at bootstrap_node.js:467:3","error_message":"Unauthorized","problem":"missmatch"}
50
+
51
+
##### JsonLogger.prototype.info(action, data)
52
+
53
+
Prints the provided data to the console in JSON format.
// {"name":"redis","action":"query","level":50,"time":"2016-08-15T08:50:23.569Z","error_name":"Error","error_stack":"Error: Unauthorized\n at Object.<anonymous> (/home/blacksonic/workspace/bunyan-debug/example.js:15:32)\n at Module._compile (module.js:541:32)\n at Object.Module._extensions..js (module.js:550:10)\n at Module.load (module.js:458:32)\n at tryModuleLoad (module.js:417:12)\n at Function.Module._load (module.js:409:3)\n at Module.runMain (module.js:575:10)\n at run (bootstrap_node.js:352:7)\n at startup (bootstrap_node.js:144:9)\n at bootstrap_node.js:467:3","error_message":"Unauthorized","problem":"missmatch"}
The separate steps of the logging process can be configured here.
126
+
These modifications affect all the instances of the library.
127
+
With transformers we can alter the data to be logged before passing to the formatter and then to the output.
128
+
It is a perfect place to add the name of the machine is running on or the request id associated with the current thread stored on a continuation local storage.
129
+
130
+
```javascript
131
+
constLogger=require('@emartech/json-logger');
132
+
133
+
Logger.configure({
134
+
formatter:JSON.stringify,
135
+
output:console.log,
136
+
transformers: []
137
+
});
138
+
139
+
```
140
+
141
+
### Log levels
142
+
143
+
- "fatal" (60): The service/app is going to stop or become unusable now.
144
+
An operator should definitely look into this soon.
145
+
- "error" (50): Fatal for a particular request, but the service/app continues
146
+
servicing other requests. An operator should look at this soon(ish).
147
+
- "warn" (40): A note on something that should probably be looked at by an
148
+
operator eventually.
149
+
- "info" (30): Detail on regular operation.
150
+
- "debug" (20): Anything else, i.e. too verbose to be included in "info" level.
151
+
- "trace" (10): Logging from external libraries used by your app or *very*
152
+
detailed application logging.
33
153
34
154
### Logging request identifier automatically
35
155
36
-
The continuation local storage handling has moved to the `@emartech/cls-adapter` package.
37
156
You need to use the middlewares of `@emartech/cls-adapter` and add its transformer to the loggers configure method.
38
-
This way it will log the request identifier coming from the header field (`X-Request-Id`).
157
+
This way it will log the request identifier coming from the header field (`X-Request-Id`) to every log line
158
+
where the called function is originating from the route handler.
39
159
40
160
For automatting
41
161
42
162
```javascript
43
163
constKoa=require('koa');
44
164
constlogFactory=require('@emartech/json-logger');
45
165
constclsAdapter=require('@emartech/cls-adapter');
166
+
constlogger=logFactory('redis');
46
167
47
168
logFactory.configure({
48
169
transformers: [
@@ -52,17 +173,11 @@ logFactory.configure({
52
173
53
174
constapp=newKoa();
54
175
app.use(clsAdapter.getKoaMiddleware());
55
-
```
56
176
57
-
## Development
58
-
59
-
While developing JSON is not the most readable format. To solve this a little
60
-
command line formatter is also included which is very familiar to [debug]'s
61
-
output format.
62
-
63
-
```
64
-
redis INFO +0ms action="connected" domain="yahoo"
65
-
redis ERROR +2ms action="query" error_message="Unauthorized" error_name="Error" error_stack="Error: Unauthorized\n at Object.<anonymous> (/home/blacksonic/workspace/bunyan-debug/example.js:15:32)\n at Module._compile (module.js:541:32)\n at Object.Module._extensions..js (module.js:550:10)\n at Module.load (module.js:458:32)\n at tryModuleLoad (module.js:417:12)\n at Function.Module._load (module.js:409:3)\n at Module.runMain (module.js:575:10)\n at run (bootstrap_node.js:352:7)\n at startup (bootstrap_node.js:144:9)\n at bootstrap_node.js:467:3" problem="missmatch"
0 commit comments