Index

lognestmonster / 84df067

A general-purpose single-header C logging library and parser for event-based logs. (Incomplete)

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
4726 Dec 2018 17:01d40e1ceUpdate README.mdJoshua1501N

Blob @ lognestmonster / README.md

text/plain17069 bytesdownload raw
1<img src="/static/logo.png" height="200px"/>
2
3_Advanced node.js logging for advanced programs._
4
51. [lognestmonster](#lognestmonster)
62. [Installation](#installation)
73. [Classes](#classes)
8 1. [Logger.Logger](#logger)
9 2. [Logger.Queue](#queue)
10 3. [Logger.Statement](#statement)
11 4. [Logger.Event](#event)
124. [Verbosity Levels](#verbosity-levels)
135. [Sample Usage](#sample-usage)
14 1. [Notes](#notes-about-this-example)
156. [Log Format](#log-format)
167. [Contributors](#contributors)
17 1. [License](#license)
18
19# lognestmonster
20[![](https://img.shields.io/github/downloads/joshuas3/log-nest-monster/total.svg)](https://github.com/JoshuaS3/log-nest-monster)
21[![](https://img.shields.io/github/issues/joshuas3/log-nest-monster.svg)](https://github.com/JoshuaS3/log-nest-monster/issues)
22[![](https://img.shields.io/github/issues-pr/joshuas3/log-nest-monster.svg)](https://github.com/JoshuaS3/log-nest-monster/pulls)
23[![](https://img.shields.io/npm/v/lognestmonster.svg)](https://www.npmjs.com/package/lognestmonster)
24[![](https://img.shields.io/npm/l/lognestmonster.svg)](https://www.npmjs.com/package/lognestmonster)
25
26Most loggers available only use a _linear_ method of logging; there are verbosity levels and tags to narrow searches, but everything is still on the same level or plane nonetheless. The package `lognestmonster` is a similar type of logger, but it allows you to create multiple **layers** (or "**nests**") of log statements. This is useful because, although you may have to put in some extra organizational work on the code side, the log results are much more clean and thorough, boosting workflow efficiency. The user has absolute control over the way log statements are pushed to their log files.
27
28_Why should I use this?_ There are many projects that create insane amounts of data, almost impossible to sift through without a helper program. The purpose of this logging system is to be a time-saver. Although it takes more time to put it in place, it's almost immediately made up with the performance gain through using the new log format. One thing that's unique about this logger compared to others is that it allows multiple queues to be made, in turn allowing you to split up your data. For example, a Node web server could keep one log file that records everything that the backend does while it uses another to record user or traffic information for analytics. Parsing software could be used to read either one.
29
30## Installation
31
32In your npm initiated package, type the following:
33```
34npm i lognestmonster
35```
36That's it! You can now `require("lognestmonster")` and begin using it.
37
38## Classes
39
40There are 4 classes offered by the package: `Logger`, the driving device that organizes everything; `Queue`, the class that actually pushes the log statements to their respective file; `Statement`, the actual log data (timestamp, verbosity, tag/invoker, message); and `Event`, a nest layer for `Statement`s or other `Event`s.
41
42The following subsections assume that `lognestmonster` has been `require`d by node.js with the following code:
43```javascript
44const Logger = require("lognestmonster");
45```
46
47### Logger
48
49Creates and organizes your queues. `Logger` is the exported package. `Logger.Logger` is the Logger class.
50
51Logger.Logger()
52```javascript
53var MyLogger = new Logger.Logger(Object config);
54```
55Where `Object config` defaults to:
56```json
57{"name": "Logger", "locations": {"node": "./log/node"}}
58```
59`name` serves no purpose other than identification. `locations` is used to create new queues, taking each key as the queue name and each value as the queue output location.
60
61Logger.Logger.queue()
62```javascript
63MyLogger.queue(string name);
64```
65This returns the appropriate `Queue` object for the provided `name`, assuming it was created with the `Logger` object.
66
67### Queue
68
69Manages log `Statement`s and `Event`s and how they're written to the final log file. Note that these are implicitly created with `Logger.Logger` when the `locations` object is properly provided in the `config` parameter of the `Logger.Logger` constructor.
70
71Logger.Queue()
72```javascript
73let MyQueue = new Logger.Queue(string name, string location);
74// note that parameters are the same format as key-value
75// pairs in `config.locations` of `Logger.Logger(config)`
76```
77This creates the queue, taking `name` to be used as its ID and `location` as the path to where the log file should be created.
78
79Logger.Queue.push()
80```javascript
81MyQueue.push(Statement statement);
82MyQueue.push(Event event);
83MyQueue.push(string verbosity, string tag, string message); // implicitly creates a `Statement` object
84```
85This adds log items or nests to the to-write queue. This returns the Queue object.
86
87Logger.Queue.write()
88```javascript
89MyQueue.write();
90```
91This appends every queue value to the log file, emptying the queue. This returns the Queue object.
92
93### Statement
94
95This is the base log item where written log data is actually held.
96
97Logger.Statement()
98```javascript
99let MyStatement = Logger.Statement(string verbosity, string tag, string message);
100```
101
102The timestamp value is created automatically by the constructor.
103
104### Event
105
106This is the proper name for a nest. Essentially, it's just an array that can hold other `Event` objects and `Statement` objects, creating a tree.
107
108Logger.Event()
109```javascript
110let MyEvent = Logger.Event();
111let MyEvent = Logger.Event(Event event);
112let MyEvent = Logger.Event(Statement statement);
113let MyEvent = Logger.Event(string verbosity, string tag, string message);
114```
115Any arguments given are passed to `this.push()`.
116
117Logger.Event.push()
118```javascript
119MyEvent.push(Event event);
120MyEvent.push(Statement statement);
121MyEvent.push(string verbosity, string tag, string message);
122```
123This message pushes an `Event` or `Statement` as items in the nest. In the case that 3 strings are given as arguments, a `Statement` is implicitly created. This returns the Event object.
124
125## Verbosity Levels
126
127When creating a `Statement`, you can pass anything you'd like for the first `verbosity` string, although there are some ones preset by the package:
128
129`Logger.INFO`
130`Logger.DEBUG`
131`Logger.VERBOSE`
132`Logger.VERYVERBOSE`
133`Logger.WARNING`
134`Logger.ERROR`
135
136These verbosity levels can be used to narrow down your search results when parsing the log files.
137
138## Sample Usage
139
140Here's an example of how somebody would initiate the Logger, create and push items to the Queue, and write them to the log file. **PLEASE SEE THE NOTES ABOUT THIS EXAMPLE DOWN BELOW.**
141```javascript
142// Require the package
143const Logger = require("lognestmonster");
144
145// Creates the logger object. Placing the new Logger inside the package allows cross-file usage, so you only have to initiate once.
146Logger.Overseer = new Logger.Logger({
147 name: "Overseer",
148 locations: {
149 "node": "./log/node" // Creates a queue named `node` that uses the path `./log/node`
150 },
151});
152
153// Pushes a statement directly to the `node` queue
154Logger.Overseer.queue("node").push(Logger.INFO, "PROCESS", "Process started");
155
156// Creates a new event and pushes a Statement to it
157let LoadEvent = new Logger.Event();
158LoadEvent.push(Logger.INFO, "INIT", "Acquiring needed top-level packages.");
159
160// Creates a new event and pushes multiple Statements to it
161let LowerNestedEvent = new Logger.Event();
162
163LowerNestedEvent.push(Logger.INFO, "INIT", "Loading fs...");
164LowerNestedEvent.push(Logger.DEBUG, "INIT", "fs loaded.");
165
166LowerNestedEvent.push(Logger.INFO, "INIT", "Loading http...");
167LowerNestedEvent.push(Logger.DEBUG, "INIT", "http loaded.");
168
169LowerNestedEvent.push(Logger.INFO, "INIT", "Loading jsonwebtoken...");
170LowerNestedEvent.push(Logger.DEBUG, "INIT", "jsonwebtoken loaded.");
171
172LowerNestedEvent.push(Logger.INFO, "INIT", "Loading lognestmonster...");
173LowerNestedEvent.push(Logger.DEBUG, "INIT", "lognestmonster loaded.");
174
175// Pushes the Event LowerNestedEvent to the Event LoadEvent
176LoadEvent.push(LowerNestedEvent);
177
178// Pushes another statement to LoadEvent
179LoadEvent.push(Logger.INFO, "INIT", "Finished.");
180
181// Pushes LoadEvent (a nest that consists of [Statement, Event, Statement] now) to the write queue
182Logger.Overseer.queue("node").push(LoadEvent);
183
184// Queue should now look like this: [Statement, Event]
185
186// Writes the queue, effectively emptying it into the log file
187Logger.Overseer.queue("node").write();
188```
189
190The above code creates the following JSON-like log output in the designated file:
191```json
192{"timestamp":"2018-12-26T18:08:37.654Z","verbosity":"INFO","tag":"PROCESS","message":"Process started"}
193[{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"INFO","tag":"INIT","message":"Acquiring needed top-level packages."},[{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"INFO","tag":"INIT","message":"Loading fs..."},{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"DEBUG","tag":"INIT","message":"fs loaded."},{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"INFO","tag":"INIT","message":"Loading http..."},{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"DEBUG","tag":"INIT","message":"http loaded."},{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"INFO","tag":"INIT","message":"Loading jsonwebtoken..."},{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"DEBUG","tag":"INIT","message":"jsonwebtoken loaded."},{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"INFO","tag":"INIT","message":"Loading lognestmonster..."},{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"DEBUG","tag":"INIT","message":"lognestmonster loaded."}],{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"INFO","tag":"INIT","message":"Finished."}]
194```
195
196_To see how to parse this data (putting it into proper JSON), see the Log Format section._
197
198### Notes about this example
199
200_Large projects_
201If your project spans multiple files, you could easily place your Logger object into the package itself, allowing the same Logger object to be accessed by other parts of your project. This is seen in the sample code with `Logger.Overseer = new Logger.Logger(...)`.
202
203_Repetition_
204There is some repetitive code; specifically, `Logger.Overseer.queue("node")`. Do note that this actually results in a `Queue` object, so you could easily make this its own variable like this:
205```javascript
206let NodeQueue = Logger.Overseer.queue("node");
207NodeQueue.push(...).write();
208```
209
210_Queue pushing and writing_
211When you're pushing multiple objects to a queue, be wary that they will stay there until the queue is written. Because everything is its own class, what you're really pushing is a _reference_ to the real object, so you can make changes to a pushed object after it has been pushed. As such, it is entirely possible to prematurely write a queue before an already-pushed `Event` or `Statement` is finished. It's good practice to immediately write the queue immediately after it has been pushed. Perhaps in the future there will be some functionality where a `Statement` or `Event` can be directly written instead of placed in the queue.
212
213_Push order_
214The developer has control over **everything** here. The order of your log file is the order that you push to your `Event`s and `Queue`. If I were to push a new Statement to `LoadEvent` in the middle of the pushes to `LowerNestedEvent`, it would show up on the log first because `LowerNestedEvent` isn't itself pushed to `LoadEvent` until later in the code. The order of the log file is 100% logical and in the developer's control, so it would be wise to write your code neatly with this in mind. This logging package doesn't do anything you don't tell it to.
215
216## Log Format
217
218Logs are controlled by `Queue` objects. Placed in the folder they're told, the logs should follow the ISO datetime format and have a `.log` file extension. Here's the name of a sample log file: `2018-12-26T18-08-37-653Z.log`
219
220Regarding format, logs follow a JSON-like format, as follows:
221```json
222{"timestamp":"2018-12-26T18:08:37.654Z","verbosity":"INFO","tag":"PROCESS","message":"Process started"}
223[{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"INFO","tag":"INIT","message":"Acquiring needed top-level packages."},[{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"INFO","tag":"INIT","message":"Loading fs..."},{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"DEBUG","tag":"INIT","message":"fs loaded."},{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"INFO","tag":"INIT","message":"Loading http..."},{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"DEBUG","tag":"INIT","message":"http loaded."},{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"INFO","tag":"INIT","message":"Loading jsonwebtoken..."},{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"DEBUG","tag":"INIT","message":"jsonwebtoken loaded."},{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"INFO","tag":"INIT","message":"Loading lognestmonster..."},{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"DEBUG","tag":"INIT","message":"lognestmonster loaded."}],{"timestamp":"2018-12-26T18:08:37.655Z","verbosity":"INFO","tag":"INIT","message":"Finished."}]
224```
225
226Because of the way the Queue objects push it (the most efficient way regarding computing power with changing/appending to files), you will have to do a bit of tweaking to get it in proper JSON format:
227
2281. Add a comma before every newline (except for the last one at the end of the file)
2292. Wrap everything in table brackets ('[' and ']')
230
231This could be easily automated. Once finished, you get proper JSON (below), where each object `{}` is a Statement and each wrapping table `[]` is an Event (excluding the outermost one).
232
233```json
234[
235 {
236 "timestamp": "2018-12-26T05:55:23.360Z",
237 "verbosity": "INFO",
238 "tag": "PROCESS",
239 "message": "Process started"
240 },
241 [
242 {
243 "timestamp": "2018-12-26T05:55:23.360Z",
244 "verbosity": "INFO",
245 "tag": "INIT",
246 "message": "Acquiring needed top-level packages."
247 },
248 [
249 {
250 "timestamp": "2018-12-26T05:55:23.360Z",
251 "verbosity": "INFO",
252 "tag": "INIT",
253 "message": "Loading fs..."
254 },
255 {
256 "timestamp": "2018-12-26T05:55:23.360Z",
257 "verbosity": "DEBUG",
258 "tag": "INIT",
259 "message": "fs loaded."
260 },
261 {
262 "timestamp": "2018-12-26T05:55:23.360Z",
263 "verbosity": "INFO",
264 "tag": "INIT",
265 "message": "Loading http..."
266 },
267 {
268 "timestamp": "2018-12-26T05:55:23.360Z",
269 "verbosity": "DEBUG",
270 "tag": "INIT",
271 "message": "http loaded."
272 },
273 {
274 "timestamp": "2018-12-26T05:55:23.360Z",
275 "verbosity": "INFO",
276 "tag": "INIT",
277 "message": "Loading jsonwebtoken..."
278 },
279 {
280 "timestamp": "2018-12-26T05:55:23.360Z",
281 "verbosity": "DEBUG",
282 "tag": "INIT",
283 "message": "jsonwebtoken loaded."
284 },
285 {
286 "timestamp": "2018-12-26T05:55:23.360Z",
287 "verbosity": "INFO",
288 "tag": "INIT",
289 "message": "Loading lognestmonster..."
290 },
291 {
292 "timestamp": "2018-12-26T05:55:23.360Z",
293 "verbosity": "DEBUG",
294 "tag": "INIT",
295 "message": "lognestmonster loaded."
296 }
297 ],
298 {
299 "timestamp": "2018-12-26T05:55:23.360Z",
300 "verbosity": "INFO",
301 "tag": "INIT",
302 "message": "Finished."
303 }
304 ]
305]
306```
307
308The above can be taken in by parsing software and create something similar to the following:
309
310```
311[[LOG FILE NAME]]
312[[LOG DATE]]
313[[LOG FILE SIZE]]
314[[LOG ITEM COUNT]]
315
316TIMESTAMP - INFO - PROCESS - Process started
317v 3 ITEMS
318 TIMESTAMP - INFO - INIT - Acquiring needed top-level packages.
319 v 6 ITEMS
320 TIMESTAMP - INFO - INIT - Loading fs...
321 TIMESTAMP - DEBUG - INIT - fs loaded.
322 TIMESTAMP - INFO - INIT - Loading http...
323 TIMESTAMP - DEBUG - INIT - http loaded.
324 TIMESTAMP - INFO - INIT - Loading jsonwebtoken...
325 TIMESTAMP - DEBUG - INIT - jsonwebtoken loaded.
326 TIMESTAMP - INFO - INIT - Finished.
327```
328
329This is exciting! You still have the ability to narrow down your results with timestamps, verbosity levels, and tags/invokers, but now you have the organization with collapsable nests to not be overwhelmed with large amounts of data at the same time.
330
331## Contributors
332
333Developer - Joshua 'joshuas3' Stockin \<joshstockin@gmail.com\> (https://www.github.com/joshuas3)
334
335Name - Patrik 'Patrola' Xop (https://github.com/PatrikXop)
336
337### License
338
339This software is licensed under version 3 of the GNU General Public License.
340
341 lognestmonster (c) 2018 Joshua 'joshuas3' Stockin
342
343 This program is free software: you can redistribute it and/or modify
344 it under the terms of the GNU General Public License as published by
345 the Free Software Foundation, either version 3 of the License, or
346 (at your option) any later version.
347
348 This program is distributed in the hope that it will be useful,
349 but WITHOUT ANY WARRANTY; without even the implied warranty of
350 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
351 GNU General Public License for more details.
352
353 You should have received a copy of the GNU General Public License
354 along with this program. If not, see <https://www.gnu.org/licenses/>.
355
356The license can be found [here](LICENSE).
357