]> git.r.bdr.sh - rbdr/cologne/blame - README.md
Update the code.
[rbdr/cologne] / README.md
CommitLineData
bfd26c00 1# Cologne
08ce17a6 2
bfd26c00
RBR
3![https://david-dm.org/rbdr/cologne.svg][dependencies-shield]
4![https://circleci.com/gh/rbdr/cologne.svg?style=shield][circle-ci-shield]
87881a5c 5
bfd26c00
RBR
6Cologne is a logger multiplexer that works mainly with a JSON format. It
7can be instantiated with several loggers, or they can be changed after
8the fact.
9
10## Usage
11
12Install from npm or add to your package.json:
87881a5c 13
2aa083b6 14```
bfd26c00 15$ npm install cologne
2aa083b6
BB
16```
17
bfd26c00 18Create an instance:
2aa083b6 19
bfd26c00
RBR
20```javascript
21require('cologne');
22
23let co = new Cologne({
24 from: "Special Worker Logger",
25 loggers: [
26 new Cologne.Logger.Console({
27 formatter: new Cologne.Formatter.Token({
28 formatString: '[{{_timestamp}}]{{_from}}: {{message}}'
29 })
30 })
31 ]
87881a5c
BB
32});
33```
34
bfd26c00
RBR
35This example would create a cologne instance with a console logger that
36uses a Token formatter. (More on loggers and formatters below.);
87881a5c 37
bfd26c00 38## Do I need anything special?
87881a5c 39
bfd26c00 40Node 4.0+
87881a5c 41
bfd26c00
RBR
42## Quick API Reference
43
44* **addLogger(logger)**: Adds a logger to the cologne instance.
45* **removeLogger(logger)**: Removes a logger from the cologne instance.
46* **buildLog(item, level, [meta])**: Generates a cologne log object as if you had
47 logged an item (it will do this automatically when you log anything.)
48 level defaults to 6. You can optionally send it an object to extend
49 the object with.
941d0901
BB
50* **log, info, notice, warn, error**: Generates a log object with the
51 appropriate severity level and sends it to all loggers.
87881a5c 52
bfd26c00 53## Loggers
87881a5c 54
bfd26c00
RBR
55Cologne loggers are any object that responds to the `#log()` method.
56This methoud should be able to receive any number of arguments and
57log them independently. Similar to how you can send multiple arguments
58to the JS console.
87881a5c 59
bfd26c00
RBR
60`#log()` will receive any number of `Cologne Log Objects`. To see what
61this format includes, check further below.
2e7f5498 62
bfd26c00 63We include two loggers out of the box:
2e7f5498 64
bfd26c00
RBR
65* `Cologne.Logger.Console` logs to the JS console
66* `Cologne.Logger.File` appends to a file
67
68### Cologne.Logger.Console
69
70This logger communicates the Javascript console. It uses the log level
71to trigger the appropriate method, so `error` logs would go to stderr
72as expected when calling `console.error`.
73
74This logger can be sent a `formatter`, which is an object that responds
75to the `#format()` method: it should get a cologne log object and respond
76with a string.
77
78```javascript
79new Cologne.Logger.Console({
80 formatter : new Cologne.Formatter.Token({
81 formatString: '[{{_timestamp}}]{{_from}}: {{message}}'
82 })
83});
2e7f5498 84```
2e7f5498 85
bfd26c00
RBR
86### Cologne.Logger.File
87
88This logger opens a writable stream to a file, to which it will append
89everything. Like the Console logger it supports a `formatter` property
90that will respond to the `#format()` method.
91
92It MUST include a `file` property on initialization, otherwise it won't
93know where to write and you'll get an exception and be sad.
94
95```javascript
96new Cologne.Logger.File({
97 file: '/var/log/server_log.log',
98 formatter : new Cologne.Formatter.Token({
99 formatString: '[{{_timestamp}}]{{_from}}: {{message}}'
100 })
101});
102```
2e7f5498 103
bfd26c00 104### More Loggers?
87881a5c 105
bfd26c00
RBR
106We're working on a socket logger. It's separate so you don't have to
107install the socket dependencies if you don't want to.
87881a5c 108
bfd26c00
RBR
109If you need to, you can roll your own. Check info on the interfaces
110below.
87881a5c 111
941d0901
BB
112You can build your own logger easily for any method of transport you find
113necessary (e.g. mail, database, twitter, etc). Any object that responds
bfd26c00 114to `#log()` is a valid logger:
87881a5c
BB
115
116```javascript
117// A valid, very minimalistic logger
bfd26c00
RBR
118let simpleLogger = {
119 log : function () {
120 for (let logObject of arguments) {
121 this._doSomeMagic(logObject);
122 }
123 },
124
125 _doSomeMagic : function (logObject) {
126 console.log(logObject + "... but magical!");
87881a5c 127 }
bfd26c00 128};
87881a5c
BB
129
130logger.addLogger(simpleLogger);
131```
132
87881a5c 133
bfd26c00
RBR
134## Formatters
135
136Cologne doesn't need formatters to work, and in fact they're optional in
137the included workers. But if you would like to make your logs prettier,
138then you can use one of the included formatters or roll your own.
139
140Formatters are objects that respond to the `#format()` method. It will
141receive a single cologne log object (see fields it includes below), and
142it should return a string. That's all there is to it.
2e7f5498 143
bfd26c00
RBR
144We include some formatters so you can get running real quicklike:
145
146* `Cologne.Formatter.Simple` a simple predefined formatter
147* `Cologne.Formatter.Token` a formatter that lets you define format
148 strings that it will use to build your final log.
149
150### Cologne.Formatter.Simple
2e7f5498
BB
151
152This is the lazy formatter, it just outputs the string in the following
153format:
154
155```
bfd26c00 156'[{{_timestamp}}][{{_levelString}}]{{_from}}: {{message}}'
2e7f5498
BB
157```
158
159Where `_timestamp` is converted to ISO.
160
bfd26c00
RBR
161#### Accepted Options
162
163* `colorize` <Boolean>: whether or not to add color. False by default.
164
165By default we don't colorize the output, but if you enable the flag this
166formatter will add a bit of color in the level string. Red for bad,
167yellow for warn, blue for info, and white for everything else.
2e7f5498 168
bfd26c00
RBR
169#### Usage
170
171```javascript
172new Cologne.Formatter.Simple({
173 colorize: true
174});
2e7f5498 175```
bfd26c00
RBR
176
177### Example Output
178
179```
180co.log("hello world");
181// -> [2016-01-21T05:50:36.505Z][INFO] Server Logger: hello world
2e7f5498 182```
87881a5c 183
bfd26c00 184### Cologne.Formatter.Token
87881a5c 185
bfd26c00
RBR
186The token formatter lets you build strings with simple tokens. When
187instantiating, you can specify a `formatString` to interpolate
188properties from the logObject. The default version looks for tokens
189inside double curly braces like `{{message}}` or `{{_level}}`. If
190you don't like it, you can specify your own.
2e7f5498 191
bfd26c00 192#### Accepted Options
2e7f5498 193
bfd26c00
RBR
194* `formatString` <String>: The string used to replace. Defaults to `"{{message}}"`
195* `replaceRule` <String>: The regex rule to use for replacement of tokens in the
2e7f5498 196 formatString. Defaults to `/{{(.*?)}}/g`
2e7f5498 197* `isoDate` <Boolean> : Whether or not to convert `_timestamp` to ISO
bfd26c00
RBR
198 date. Defaults to true. Otherwise it'll use the raw timestamp.
199
200#### Usage
201
202```javascript
203new Cologne.Formatter.Token({
204 formatString: '[{{_timestamp}}]{{_from}}: {{message}}'
205});
206```
207
208#### ANSI tokens
209
210If you want to add color to your logs, you can use the special \_ansi
211token. It has several options which you can call like `{{_ansi:red}}`
212and `{{_ansi:reset}}`. Here's a list of all the ansi stuff you can use:
213
214* `bold`: makes text bold
215* `italics`: makes text italics
216* `underline`: makes text underlined
217* `inverse`: inverts foreground and background
218* `strikethrough`: strikethrough text
219* `bold_off`, `italics_off`, `underline_off`, `inverse_off`, and
220 `strikethrough_off`: turn off the specified effect.
221* `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white`,
222 and `default`: change the foreground color of your text.
223* `black_bg`, `red_bg`, `green_bg`, `yellow_bg`, `blue_bg`, `magenta_bg`,
224 `cyan_bg`, `white_bg`, and `default_bg`: change the background color of your
225 text.
226* `reset`: makes everything normal again.
227* `_level`: this is a special code that will set a color depending on
228 the level of the log: debug gets green, info and notice blue, warn is
229 yellow, and anything worse is red.
230
231### More Formatters?
232
233You can create your own formatters by creating an object that responds
234to the `#format()` method, knows how to handle cologne log objects and
235returns a string.
236
237Here's an example of a logger that surrounds a log with sparkles:
87881a5c
BB
238
239```javascript
87881a5c 240var simpleFormatter = {
bfd26c00
RBR
241 format : function (logObject) {
242 return '✨' + logObject.message + '✨';
87881a5c
BB
243 }
244}
245
bfd26c00
RBR
246logger.addLogger(new Cologne.Logger.Console({
247 formatter: simpleFormatter
87881a5c
BB
248}));
249```
250
bfd26c00
RBR
251## The Cologne Log Format
252
253The cologne log format is a JSON based log format, based on the cobalt
254log format, which is inturn based on Graylog's GELF. However, where GELF
255treats all internal fields without a prefix, and all user fields with a
256prefix we do it backwards so it's easier to extend the object with
257metadata from existing objects. Besides, we'll probably write the
258default keys automatically so you shouldn't have to do that extra work.
87881a5c 259
bfd26c00
RBR
260You could try to build it on your own, but you can use `#buildLog()`
261to build it without logging.
941d0901 262
bfd26c00 263### Fields
87881a5c 264
bfd26c00
RBR
265* **\_timestamp** : A timestamp in miliseconds with fractions of a second
266 in the floating point area.
267* **\_cologneLog** <String> : This is how we know if the log is already
268 formatted and ready to go. This field is a string containing the
269 version of cologne log format it's using. It's `1.0.0` right now.
270* **\_from**: The sender of the log (Defaults to Generic Cologne Logger)
271* **\_level**: The level of the log (Defaults to 6)
272* **\_levelString**: The string corresponding to the log level (e.g. 7 ->
273 debug, 3 -> error, 0 -> emerg)
87881a5c 274
bfd26c00 275### A word on Log Levels
87881a5c 276
bfd26c00
RBR
277The log levels in cologne correspond to the syslog levels, and the
278levelStrings correspond to the priority keywords:
87881a5c 279
bfd26c00
RBR
280* `0 -> emerg`
281* `1 -> alert`
282* `2 -> crit`
283* `3 -> error`
284* `4 -> warning`
285* `5 -> notice`
286* `6 -> info`
287* `7 -> debug`
288
289This is useful when deciding how to log. You could even have a logger
290filter out unnecessary levels (eg. If you have a reporting logger that
291only reports error or worse.)
292
293## Further Improvements
294
295* Improve the API for buildLog
296* More loggers & formatters (will not be distributed in core cologne)
297* Improve tests
298
299[dependencies-shield]: https://david-dm.org/rbdr/cologne.svg
300[circle-ci-shield]: https://circleci.com/gh/rbdr/cologne.svg?style=shield