UNPKG

6.35 kBMarkdownView Raw
1![Build Status](https://travis-ci.org/logzio/logzio-nodejs.svg?branch=master)
2
3# logzio-nodejs
4NodeJS logger for Logz.io.
5The logger stashes the log messages you send into an array which is sent as a bulk once it reaches its size limit (100 messages) or time limit (10 sec) in an async fashion.
6It contains a simple retry mechanism which upon connection reset (server side) or client timeout, wait a bit (default interval of 2 seconds), and try this bulk again. It does not block other messages from being accumulated and sent (async). The interval increases by a factor of 2 between each retry until it reaches the maximum allowed attempts (3).
7
8 By default, any error is logged to the console. This can be changed by supplying a callback function.
9
10
11## Sample usage
12```javascript
13var logger = require('logzio-nodejs').createLogger({
14 token: '__YOUR_ACCOUNT_TOKEN__',
15 type: 'YourLogType' // OPTIONAL (If none is set, it will be 'nodejs')
16});
17
18
19// sending text
20logger.log('This is a log message');
21
22// sending an object
23var obj = {
24 message: 'Some log message',
25 param1: 'val1',
26 param2: 'val2'
27};
28logger.log(obj);
29```
30
31**Note:** If logzio-js is used as part of a serverless service (AWS Lambda, Azure Functions, Google Cloud Functions, etc.), add `logger.sendAndClose()` at the end of the run. For example [sync Lambda](https://github.com/logzio/logzio-nodejs/blob/master/Serverless/lambda-sync.md) and [async Lambda](https://github.com/logzio/logzio-nodejs/blob/master/Serverless/lambda-async.md)
32
33## Options
34
35* **token**
36 Mandatory. Your account token. Look it up in the Device Config tab in Logz.io
37* **type** - Log type. Help classify logs into different classifications
38* **protocol** - `http`, `https` or `udp`. Default: `http`
39* **host** - Destination host name. Default: `listener.logz.io`
40* **port** - Destination port. Default port depends on protocol. For `udp` default port is `5050`, for `http` is `8070` and `8071` is for `https`
41* **sendIntervalMs** - Time in milliseconds to wait between retry attempts. Default: `2000` (2 sec)
42* **bufferSize** - The maximum number of messages the logger will accumulate before sending them all as a bulk. Default: `100`.
43* **numberOfRetries** - The maximum number of retry attempts. Default: `3`
44* **debug** - Should the logger print debug messages to the console? Default: `false`
45* **callback** - A callback function called when an unrecoverable error has occured in the logger. The function API is: function(err) - err being the Error object.
46* **timeout** - The read/write/connection timeout in milliseconds.
47* **addTimestampWithNanoSecs** - Add a timestamp with nano seconds granularity. This is needed when many logs are sent in the same millisecond, so you can properly order the logs in kibana. The added timestamp field will be `@timestamp_nano` Default: `false`
48* **compress** - If true the the logs are compressed in gzip format. Default: `false`
49* **internalLogger** - set internal logger that supports the function log. Default: console.
50* **setUserAgent** - Set `false` to send logs without user-agent field in request header. Default:`true`.
51* **extraFields** - Adds your own custom fields to each log. Add in JSON Format, for example: `extraFields : { field_1: "val_1", field_2: "val_2" , ... }`.
52
53
54## Using UDP
55A few notes are worth mentioning regarding the use of the UDP protocol:
56* UDP has some limitations, and therefore it is not the recommended protocol:
57 * There is no guarantee that the logs have been received.
58 * UDP can't take advantage of the bulk API, so performance is sub-optimal.
59* When using UDP, each message is sent separately, and not using the bulk API. This means that the meaning of `bufferSize` is slightly different in this case. The messages will still be sent separately, but the logger will wait for the buffer to reach the size specified before sending out all the messages. If you want each message to be sent out immediately, then set `bufferSize = 1`.
60
61
62## Update log
63**2.1.4**
64- Replace from request to axios
65
66**2.0.4**
67- Add parameter to manage User-agent
68
69**2.0.3**
70- Add verbose logging to use in Azure serverless function
71
72**2.0.2**
73- Updated required fields for typescript
74
75**2.0.1**
76- Fixed sorting by nanosec-timestamp
77- Added option to log string with an object
78- Updated Typescript declaration for optional dependencies
79
80**2.0.0**
81- Added support for TypeScript
82- End of support for node 6
83- Upgrade dependencies due to security vulnerabilities
84
85<details>
86 <summary markdown="span"> Expand to check old versions </summary>
87
88**1.0.4 - 1.0.6**
89- Upgrade dependencies due to security vulnerabilities
90
91**1.0.3**
92- Added the bulk to the callback in case the send failed
93
94**1.0.2**
95- Handle no Error code on bad requests
96
97**1.0.1**
98- ES6
99- Support node greater than node 6
100- Added gzip compress option
101- Added internal logger option
102
103**0.4.14**
104- UDP callback bug fix + tests
105- UDP close connection bug fix + tests
106- ESLint
107
108**0.4.12**
109- Updated ability to add custom port
110
111**0.4.6**
112- Updated moment (v2.19.3) and request (v2.81.0) packages
113
114**0.4.4**
115- `@timestamp` and `@timestamp_nano` will no longer be overriden given a custom value by the user.
116
117**0.4.3**
118- Add the `@timestamp` field to the logs on the client's machine (and not when it reaches the server)
119
120**0.4.1**
121- Updated `request` dependency to 2.75.0
122
123**0.4.0**
124- Fixed issue #12 - added support for UDP
125- Minor refactorings
126
127**0.3.10**
128- Fixed issue #17 - sendAndClose() wasn't actually closing the timer
129
130**0.3.9**
131- Added option to add a timestamp with nano second granularity
132
133**0.3.8**
134- Updated listener url
135- Added `sendAndClose()` method which immediately sends the queued messages and clears the global timer
136- Added option to supress error messages
137
138**0.3.6**
139- Fixed URL for github repository in package.json
140
141**0.3.5**
142- Bug fix : upon retry (in case of network error), the message gets sent forever
143
144**0.3.4**
145- Bug fix : `jsonToString()` was throwing an error in the catch()block
146
147**0.3.2**
148- Enhancement : Added option to attach extra fields to each log in a specific instance of the logger.
149
150**0.3.1**
151- Bug fix : When calling `log` with a string parameter, the object isn't constructed properly.
152
153</details>
154
155# Scripts
156
157- run `npm install` to install required dependencies
158- run `npm test` to run unit tests