Skip to content

Commit a9c7ce0

Browse files
committed
feat(json-logger-js): initial commit
BREAKING CHANGE: first deploy
1 parent f7c577c commit a9c7ce0

29 files changed

+4177
-1
lines changed

.gitignore

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
2+
# Created by https://www.gitignore.io/api/osx,node,linux,intellij+all,visualstudiocode
3+
4+
### Intellij+all ###
5+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
6+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
7+
8+
# User-specific stuff:
9+
.idea/**/workspace.xml
10+
.idea/**/tasks.xml
11+
.idea/dictionaries
12+
13+
# Sensitive or high-churn files:
14+
.idea/**/dataSources/
15+
.idea/**/dataSources.ids
16+
.idea/**/dataSources.xml
17+
.idea/**/dataSources.local.xml
18+
.idea/**/sqlDataSources.xml
19+
.idea/**/dynamic.xml
20+
.idea/**/uiDesigner.xml
21+
22+
# Gradle:
23+
.idea/**/gradle.xml
24+
.idea/**/libraries
25+
26+
# CMake
27+
cmake-build-debug/
28+
29+
# Mongo Explorer plugin:
30+
.idea/**/mongoSettings.xml
31+
32+
## File-based project format:
33+
*.iws
34+
35+
## Plugin-specific files:
36+
37+
# IntelliJ
38+
/out/
39+
40+
# mpeltonen/sbt-idea plugin
41+
.idea_modules/
42+
43+
# JIRA plugin
44+
atlassian-ide-plugin.xml
45+
46+
# Cursive Clojure plugin
47+
.idea/replstate.xml
48+
49+
# Crashlytics plugin (for Android Studio and IntelliJ)
50+
com_crashlytics_export_strings.xml
51+
crashlytics.properties
52+
crashlytics-build.properties
53+
fabric.properties
54+
55+
### Intellij+all Patch ###
56+
# Ignores the whole idea folder
57+
# See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360
58+
59+
.idea/
60+
61+
### Linux ###
62+
*~
63+
64+
# temporary files which can be created if a process still has a handle open of a deleted file
65+
.fuse_hidden*
66+
67+
# KDE directory preferences
68+
.directory
69+
70+
# Linux trash folder which might appear on any partition or disk
71+
.Trash-*
72+
73+
# .nfs files are created when an open file is removed but is still being accessed
74+
.nfs*
75+
76+
### Node ###
77+
# Logs
78+
logs
79+
*.log
80+
npm-debug.log*
81+
yarn-debug.log*
82+
yarn-error.log*
83+
84+
# Runtime data
85+
pids
86+
*.pid
87+
*.seed
88+
*.pid.lock
89+
90+
# Directory for instrumented libs generated by jscoverage/JSCover
91+
lib-cov
92+
93+
# Coverage directory used by tools like istanbul
94+
coverage
95+
96+
# nyc test coverage
97+
.nyc_output
98+
99+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
100+
.grunt
101+
102+
# Bower dependency directory (https://bower.io/)
103+
bower_components
104+
105+
# node-waf configuration
106+
.lock-wscript
107+
108+
# Compiled binary addons (http://nodejs.org/api/addons.html)
109+
build/Release
110+
111+
# Dependency directories
112+
node_modules/
113+
jspm_packages/
114+
115+
# Typescript v1 declaration files
116+
typings/
117+
118+
# Optional npm cache directory
119+
.npm
120+
121+
# Optional eslint cache
122+
.eslintcache
123+
124+
# Optional REPL history
125+
.node_repl_history
126+
127+
# Output of 'npm pack'
128+
*.tgz
129+
130+
# Yarn Integrity file
131+
.yarn-integrity
132+
133+
# dotenv environment variables file
134+
.env
135+
136+
137+
### OSX ###
138+
*.DS_Store
139+
.AppleDouble
140+
.LSOverride
141+
142+
# Icon must end with two \r
143+
Icon
144+
145+
# Thumbnails
146+
._*
147+
148+
# Files that might appear in the root of a volume
149+
.DocumentRevisions-V100
150+
.fseventsd
151+
.Spotlight-V100
152+
.TemporaryItems
153+
.Trashes
154+
.VolumeIcon.icns
155+
.com.apple.timemachine.donotpresent
156+
157+
# Directories potentially created on remote AFP share
158+
.AppleDB
159+
.AppleDesktop
160+
Network Trash Folder
161+
Temporary Items
162+
.apdisk
163+
164+
### VisualStudioCode ###
165+
.vscode/*
166+
!.vscode/settings.json
167+
!.vscode/tasks.json
168+
!.vscode/launch.json
169+
!.vscode/extensions.json
170+
.history
171+
172+
# End of https://www.gitignore.io/api/osx,node,linux,intellij+all,visualstudiocode
173+
174+
### Project ###
175+
/tmp/

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.spec.js
2+
node_modules
3+
.idea
4+
tmp

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8.4.0

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,46 @@
1-
# json-logger-js
1+
# json-logger-js
2+
3+
Simple JSON logger middleware that combines the namespaces of [debug] and the
4+
machine readable JSON format of [bunyan].
5+
6+
It has the same logging levels as [bunyan].
7+
8+
## Example
9+
10+
```javascript
11+
process.env.DEBUG = 'redis';
12+
let mongoLogger = require('bunyan-debug')('mongo');
13+
let redisLogger = require('bunyan-debug')('redis');
14+
15+
// simple info logging with enabled namespace
16+
redisLogger.info('connected', { domain: 'yahoo' });
17+
18+
// not enabled
19+
mongoLogger.info('connected', { domain: 'google' });
20+
21+
// error objects
22+
redisLogger.fromError('query', new Error('Unauthorized'), { problem: 'missmatch' });
23+
```
24+
25+
will output
26+
27+
```
28+
{"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z","domain":"yahoo"}
29+
{"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"}
30+
```
31+
32+
Examples can be found in ```example.js```.
33+
34+
## Development
35+
36+
While developing JSON is not the most readable format. To solve this a little
37+
command line formatter is also included which is very familiar to [debug]'s
38+
output format.
39+
40+
```
41+
redis INFO +0ms action="connected" domain="yahoo"
42+
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"
43+
```
44+
45+
[debug]: https://github.com/visionmedia/debug
46+
[bunyan]: https://github.com/trentm/node-bunyan

bin/json-logger.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#! /usr/bin/env node
2+
'use strict';
3+
let eventStream = require('event-stream');
4+
let transformLine = require('../src/output/transform/transform');
5+
6+
process.stdin.setEncoding('utf8');
7+
process.stdin
8+
.pipe(eventStream.split())
9+
.pipe(eventStream.mapSync(transformLine))
10+
.pipe(process.stdout);

example.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
process.env.DEBUG = 'redis';
3+
let logger = require('./index');
4+
5+
let mongoLogger = logger('mongo');
6+
let redisLogger = logger('redis');
7+
8+
// simple info logging with enabled namespace
9+
redisLogger.info('connected', { domain: 'yahoo' });
10+
11+
// not enabled
12+
mongoLogger.info('connected', { domain: 'google' });
13+
14+
// error objects
15+
redisLogger.fromError('query', new Error('Unauthorized'), { problem: 'missmatch' });
16+
17+
// displays as is
18+
console.log(JSON.stringify({ example: 'output' }));

index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const Logger = require('./src/logger/logger');
4+
const Timer = require('./src/timer/timer');
5+
const isNamespaceEnabled = require('./src/enabled/enabled');
6+
const contextMiddlewareFactory = require('./src/context-middleware-factory/context-middleware-factory');
7+
8+
/**
9+
* @param namespace
10+
* @param options
11+
* @returns {Logger}
12+
*/
13+
function logFactory(namespace, options) {
14+
return new Logger(namespace, isNamespaceEnabled(
15+
logFactory.getNamespaces(), namespace
16+
), options);
17+
}
18+
19+
logFactory.Logger = Logger;
20+
logFactory.Timer = Timer;
21+
logFactory.getNamespaces = function() {
22+
return process.env.DEBUG || '';
23+
};
24+
logFactory.getMiddleware = contextMiddlewareFactory.getMiddleware;
25+
26+
module.exports = logFactory;

koaexample.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const Koa = require('koa');
4+
const logFactory = require('./index');
5+
const logger = logFactory('example');
6+
const port = 3000;
7+
8+
const app = new Koa();
9+
10+
app.use(async (ctx, next) => {
11+
ctx.request.header['x-request-id'] = 'uuid';
12+
await next();
13+
});
14+
app.use(logFactory.getMiddleware());
15+
app.use(async () => {
16+
logger.info('works');
17+
});
18+
19+
app.listen(port);
20+
console.log('listening on port: ' + port);

0 commit comments

Comments
 (0)