|
| 1 | +# Streams Logger |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/@tsxper/log-stream) |
| 4 | +[](LICENSE) |
| 5 | + |
| 6 | +[](https://www.npmjs.com/package/@tsxper/log-stream) |
| 7 | + |
| 8 | +*@tsxper/log-stream* is a TypeScript stream-based NodeJS logger. |
| 9 | + |
| 10 | +Main features of the *@tsxper/log-stream* are: |
| 11 | +- simplicity; |
| 12 | +- small size; |
| 13 | +- "compact" and colored "visual" output formats; |
| 14 | +- logging scopes; |
| 15 | +- logging data structures for debugging; |
| 16 | +- easy streams replacement; |
| 17 | +- circular references safety; |
| 18 | +- easy scopes spreading through cloning; |
| 19 | + |
| 20 | +## Configuration |
| 21 | + |
| 22 | +### Output Formats |
| 23 | + |
| 24 | +Currently 2 output formats are supported: "compact" (default) and "visual" (colored or not) |
| 25 | + |
| 26 | +#### Visual Format |
| 27 | + |
| 28 | + |
| 29 | +#### Compact Format |
| 30 | + |
| 31 | + |
| 32 | +Compact format contains a JSON string on a new line. |
| 33 | +Compact format message decoded: |
| 34 | +- t [required], time (ms); |
| 35 | +- l [required], log level; |
| 36 | +- s [required], scope; |
| 37 | +- n [required], name (log message); |
| 38 | +- d [optional], data; |
| 39 | +- e [optional], error; |
| 40 | + |
| 41 | +```JavaScript |
| 42 | +const logger = new Logger(LOG_LEVEL_INFO, 'my service scope'); |
| 43 | +logger.setOutputFormat('visual'); |
| 44 | +logger.setColors(false); // disable colors for 'visual' output format |
| 45 | +// logger.setColors(true); // applies only to 'visual' output format |
| 46 | +// logger.setOutputFormat('compact'); // default |
| 47 | +``` |
| 48 | + |
| 49 | +### Log Levels |
| 50 | +Possible log levels are: |
| 51 | +- *LOG_LEVEL_NONE*, or 0, no logs are logged; |
| 52 | +- *LOG_LEVEL_ERROR*, or 1, only "error" logs are logged; |
| 53 | +- *LOG_LEVEL_WARN*, or 2, "error" and "warn" logs are logged; |
| 54 | +- *LOG_LEVEL_INFO*, or 3, "error", "warn" and "info" logs will be logged; |
| 55 | +- *LOG_LEVEL_DEBUG*, or 4, "error", "warn", "info" and "debug" logs will be logged; |
| 56 | + |
| 57 | +```JavaScript |
| 58 | +// JS example |
| 59 | +logger.debug('debug message', [1, 2]); |
| 60 | +logger.log('info message'); |
| 61 | +logger.warn('warn message'); |
| 62 | +logger.error('error message', new Error('error')); |
| 63 | +``` |
| 64 | + |
| 65 | +```TypeScript |
| 66 | +// TS interface |
| 67 | +debug(name: string, data?: unknown): void; |
| 68 | +log(name: string, data?: unknown): void; |
| 69 | +warn(name: string, data?: unknown): void; |
| 70 | +error(name: string, data: Error): void; |
| 71 | +``` |
| 72 | + |
| 73 | +### Log Scopes |
| 74 | +Log scope can be set in a constructor or later in *setScope()* |
| 75 | + |
| 76 | +```TypeScript |
| 77 | +constructor(logLevel?: LOG_LEVEL, scope?: string); |
| 78 | +setScope(scope: string): this; |
| 79 | +``` |
| 80 | + |
| 81 | +### Logging Objects |
| 82 | +In "compact" output format all *data* objects are converted into JSON strings and are logged "as is". |
| 83 | +In "visual" output format or when *data* objects contains circular refs, a "depth" parameter is applied |
| 84 | +(by default depth=2). |
| 85 | + |
| 86 | +```TypeScript |
| 87 | +setDepth(depth: number): this; |
| 88 | +``` |
| 89 | + |
| 90 | +**Circular Refs**. |
| 91 | +In case passed *data* object contains circular refs, such object is converted into its string representation with denoted circular references. |
| 92 | + |
| 93 | +### Log Streams |
| 94 | +Two logs streams are supported: general logs stream and error log stream. |
| 95 | +Default log stream for error logs is *process.stderr*. |
| 96 | +Default log stream for general logs (all logs that are not errors) is process.stdout. |
| 97 | + |
| 98 | +```TypeScript |
| 99 | +// replace log streams |
| 100 | +static replaceLogStreams(stdOut: NodeJS.WritableStream, stdErr: NodeJS.WritableStream): void; |
| 101 | +``` |
| 102 | +Calling *Logger.replaceLogStreams()* will make all existing *Logger* instances write into the new streams. |
| 103 | + |
| 104 | +### Clone Logger |
| 105 | +Cloning a logger instance makes easy to create a new logger with a new scope but existing setting. |
| 106 | + |
| 107 | +```JavaScript |
| 108 | +const logger2 = logger1.clone('new scope'); |
| 109 | +``` |
| 110 | + |
| 111 | +### Formatters |
| 112 | +Output formats are configurable through setting custom formatting functions. |
| 113 | + |
| 114 | +```TypeScript |
| 115 | +setFormatterCompact(formatter: LOG_FORMATTER): this; |
| 116 | +setFormatterVisual(formatter: LOG_FORMATTER): this; |
| 117 | +``` |
0 commit comments