1 | # justlog
|
2 | [![Build Status](https://api.travis-ci.org/q3boy/justlog.png?branch=master)](http://travis-ci.org/q3boy/justlog)
|
3 |
|
4 | justlog , focus on logging
|
5 |
|
6 | ## Features:
|
7 |
|
8 | * coloured console logging
|
9 | * file appender, with log rotate based on time
|
10 | * configurable log message/patterns
|
11 | * different log levels for different log categories (info debug warn error )
|
12 | * can use as a connect middleware for access-log
|
13 |
|
14 | ## Installation
|
15 |
|
16 | `npm install justlog`
|
17 |
|
18 |
|
19 | ## Usage
|
20 |
|
21 | ### Getting start
|
22 | ```javascript
|
23 | // simple.js
|
24 | var justlog = require('justlog');
|
25 |
|
26 | var log = justlog();
|
27 | l.info('simple', 'info');
|
28 | l.debug({a:1, b:2});
|
29 | l.warn([1,2,3,4]);
|
30 | l.error('name:%s, number:%d', 'somename', 123);
|
31 | ```
|
32 |
|
33 | ```shell
|
34 | $ node simple.js
|
35 | ```
|
36 |
|
37 | the stdout/stderr is as below
|
38 | ```javascript
|
39 | 19:01:41 INFO a.js:4 simple info
|
40 | 19:01:41 DEBUG a.js:5 {"a":1,"b":2}
|
41 | 19:01:41 WARN a.js:6 1,2,3,4
|
42 | 19:01:41 ERROR a.js:7 name:somename, number:123
|
43 | ```
|
44 | and also, you can got a log file on `logs/simple-%Y-%M-%D.log`
|
45 |
|
46 | ```javascript
|
47 | 2013-01-06 19:01:41 [WARN] (a.js:6) 1,2,3,4
|
48 | 2013-01-06 19:01:41 [ERROR] (a.js:7) name:somename, number:123
|
49 | ```
|
50 |
|
51 |
|
52 | ### Change log levels
|
53 |
|
54 | ```javascript
|
55 | var log = justlog({
|
56 | file : {level: justlog.INFO | justlog.WARN} // file log levels
|
57 | stdio : {level: justlog.DEBUG | justlog.ERROR} // stdio log levels
|
58 | });
|
59 | ```
|
60 | available levels
|
61 |
|
62 | * justlog.INFO
|
63 | * justlog.DEBUG
|
64 | * justlog.WARN
|
65 | * justlog.ERROR
|
66 | * justlog.ALL include all 4 level above
|
67 | * justlog.EXCEPTION include WARN and ERROR
|
68 |
|
69 | ### Log only one way
|
70 | ```javascript
|
71 | var stdLog = justlog({file : false}); // not write log file, only use stdio
|
72 | var fileLog = justlog({stdio : false}); // not use stdio, only write a log file
|
73 | });
|
74 | ```
|
75 |
|
76 | ### log error & warn messages into stdout
|
77 |
|
78 | ```javascript
|
79 | var log = justlog({
|
80 | stdio: {
|
81 | stderr: process.stdout
|
82 | }
|
83 | });
|
84 | ```
|
85 |
|
86 | ### custom log file path & log file rotate
|
87 | ```javascript
|
88 | // write logs into filename.log and never rotate
|
89 | var log1 = justlog({
|
90 | file: {
|
91 | path : '[filename.log]'
|
92 | }
|
93 | });
|
94 |
|
95 | // write logs into filename-%Y-%M-%D.log, and rotate every days
|
96 | var log2 = justlog({
|
97 | file: {
|
98 | path : '[filename]-YYYY-MM-DD[.log]'
|
99 | }
|
100 | });
|
101 | ```
|
102 | time format use [moment string-format](http://momentjs.com/docs/#/parsing/string-format/)
|
103 |
|
104 | rotate will be trigger when log filename changed.
|
105 |
|
106 | ### custom log line format
|
107 | ```javascript
|
108 | var log = justlog({
|
109 | file : {
|
110 | pattern : '{fulltime} [{level}] {msg}' // use custom pattern
|
111 | }
|
112 | stdio : {
|
113 | pattern : 'simple-color' // use predefined pattern
|
114 | }
|
115 | });
|
116 | log.warn('log msg');
|
117 | ```
|
118 | ## All options
|
119 | ```javascript
|
120 | {
|
121 | file : {
|
122 | level : error | warn, // levels for filelog
|
123 | pattern : 'file', // filelog pattern
|
124 | path : '[logs/%main_script_name]YYYY-MM-DD[.log]', // log file path
|
125 | mode : '0664', // log file mode
|
126 | dir_mode : '2775', // if log dir is not exists, log dir mode when create
|
127 | _watcher_timeout : 5007, // log file renamed watch timeout, DO NOT CHANGE THIS IF YOU REALLY KNOWN WHAT YOU DO.
|
128 | },
|
129 | stdio : {
|
130 | level : error | warn | debug | info, // levels for stdio
|
131 | pattern : 'color', // stdio pattern
|
132 | stdout : process.stdout, // stdout stream (for info & debug log)
|
133 | stderr : process.stderr, // stderr stream (for warn & error log)
|
134 | }
|
135 | }
|
136 | ```
|
137 |
|
138 | ## Middleware
|
139 | a connect middleware for apache-like accesslog.
|
140 |
|
141 | middleware's has same options as normally justlog object, but has different default value
|
142 |
|
143 | ```javascript
|
144 | var app = connect();
|
145 | app.use(justlog.middleware({}));
|
146 | ```
|
147 | ### Middleware default options
|
148 | ```javascript
|
149 | {
|
150 | file: {
|
151 | path : '[logs/%main_script_name-access-]YYYY-MM-DD[.log]',
|
152 | pattern : 'accesslog-rt'
|
153 | }
|
154 | stdio :
|
155 | pattern : 'accesslog-color'
|
156 | }
|
157 | ```
|
158 |
|
159 | ## Buffer Log
|
160 | you can buffer your log if you have big visits.
|
161 |
|
162 | ```javascript
|
163 | var log = justlog({
|
164 | duration : 1000, // flush buffer time, default is 1000
|
165 | bufferLength : 1000 // max buffer length, default is 0
|
166 | //... other options
|
167 | });
|
168 | ```
|
169 |
|
170 | or
|
171 |
|
172 | ```javascript
|
173 | var log = justlog.create({
|
174 | duration : 1000, // flush buffer time, default is 1000
|
175 | bufferLength : 1000 // max buffer length, default is 0
|
176 | });
|
177 | ```
|
178 |
|
179 | ### default options
|
180 | ```javascript
|
181 | {
|
182 | duration : 1000,
|
183 | bufferLength : 0,
|
184 | // file: ...
|
185 | // stdio : ...
|
186 | }
|
187 | ```
|
188 |
|
189 | ### close buffer log
|
190 |
|
191 | ```javascript
|
192 | justlog.end(cb);
|
193 | ```
|
194 |
|
195 |
|
196 |
|
197 | ### About log pattern
|
198 |
|
199 | Justlog has a powerful log line pattern support.
|
200 | You can use variables, objects and functions in you patterns.
|
201 | And you can define ansi color output patterns easily.
|
202 |
|
203 |
|
204 | #### Syntax
|
205 | `{var[ args...][@colors...]}`
|
206 |
|
207 | * "{variable_name}": show varaible's value.
|
208 | eg. `{remote-address}`
|
209 | * "{object_name.prop_name}": show property value.
|
210 | eg. `{headers.accepted-encoding}`
|
211 | * "{function_name "const1", "const2"}": show function's return value with const arguments.
|
212 | eg. `{now "YYYY-MM-DD"}`
|
213 | * "{function_name variable_name, object_name.prop_name}": show function's return value with variable arguments.
|
214 | eg. `{color.status status-code}`
|
215 | * "{something@color1,color2}": set output ansi color.
|
216 | eg. `{url@blue,underline}`
|
217 |
|
218 | #### Predefine variables
|
219 |
|
220 | * msg : (all log arguments).toString()
|
221 | * level : log level text align ("INFO ", "DEBUG", "WARN ", "ERROR")
|
222 | * levelTrim : log level text without align ("INFO", "DEBUG", "WARN", "ERROR")
|
223 | * file : log triggered file path
|
224 | * lineno : log triggered code line number
|
225 | * stack : alias for "file:lineno"
|
226 | * stackColored : alias for colored stack
|
227 | * time : time format as "HH:mm:ss"
|
228 | * date : time format as "YYYY-MM-DD"
|
229 | * fulltime : time format as "YYYY-MM-DD HH:mm:ss"
|
230 | * numbertime : time format as "YYYYMMDDHHmmss"
|
231 | * mstimestamp : unix timestamp (with milliseconds)
|
232 | * timestamp : unix timestamp (with seconds)
|
233 |
|
234 | #### Predefined functions
|
235 |
|
236 | * now : now formater function. eg. `{now "YYYY-MM-DD"}`
|
237 | * color.status : add color for http status code. eg. `{color.status status}`
|
238 | * color.method : add color for http request method. eg. `{color.method method}`
|
239 | * color.event : add color for event type. eg. `{color.event event}`
|
240 | * color.level : add color for log level. eg. `{color.level level}`
|
241 |
|
242 | #### Predefined patterns
|
243 |
|
244 | * simple-color: log message and colored level text
|
245 | * simple-nocolor: like simple without color
|
246 | * color: tracestack, time, log message and colored level text
|
247 | * nocolor: like color without color
|
248 | * event-color: time, log message and colored event
|
249 | * event-nocolor: like event-color without color
|
250 | * file : fulltime, tracestack, log message and level text
|
251 | * accesslog: apache access-log
|
252 | * accesslog-rt: like access-log with response-time on the end (with microsecond)
|
253 | * accesslog-color: like accesslog-rt with ansi colored
|
254 |
|
255 | ## License
|
256 |
|
257 | `justlog` is published under BSD license.
|