Another logger in JS. This one offers a console.log-like API and formatting, colored lines and timestamps (or not if desired), all that with 0 dependencies.

Commit 0f78d008

0f78d0088539Merged

Maxence Lecanu <[email protected]> · 1 year ago · 2 files · ~2 modified

Description

chore: improve README to document plugin system

Details

Commit
0f78d0088539
Parent
560ca4a8826c

README.mdmodifiedView file

@@ -1,4 +1,4 @@
1-# hellog
1+# Hellog
22
33 _Your new logger!_
44
@@ -17,37 +17,92 @@
1717 Import `hellog` into your project and start logging:
1818
1919 ```javascript
20-const hellog = require('@steffthestunt/hellog');
20+import Hellog from '@steffthestunt/hellog';
2121
22-hellog.log('Hello, world!');
23-hellog.info('This is an info message.');
24-hellog.warn('This is a warning message.');
25-hellog.error('This is an error message.');
22+// Initialize with default config
23+const logger = new Hellog();
24+
25+logger.debug('Hello, world!');
26+// Outputs : 2024-12-21T13:48:08.064Z [INFO] Hello, world!
27+
28+logger.info('This is an info message.');
29+// Outputs : 2024-12-21T13:48:08.064Z [INFO] This is an info message.
30+
31+logger.warn('This is a warning message.');
32+// Outputs : 2024-12-21T13:48:08.064Z [WARN] This is a warning message.
2633
27-// Accepts any input
28-hellog.log('Logging a number:', 123);
29-hellog.log('Logging an object:', { key: 'value' });
30-hellog.log('Logging an array:', [1, 2, 3]);
34+logger.error('This is an error message.');
35+// Outputs : 2024-12-21T13:48:08.064Z [ERROR] This is an error message.
36+
37+logger.info('Logging a number:', 123);
38+// Outputs : 2024-12-21T13:48:08.064Z [INFO] Logging a number: 123
39+
40+logger.info('Logging an object:', { key: 'value' });
41+// Outputs : 2024-12-21T13:48:08.064Z [INFO] Logging an object: { key: 'value' }
42+
43+logger.info('Logging an array:', [1, 2, 3]);
44+// Outputs : 2024-12-21T13:48:08.064Z [INFO] Logging an array: [ 1, 2, 3 ]
3145 ```
3246
3347 ## Features
3448
35-- **Console-like API**: Use `log`, `info`, `warn`, and `error` methods.
49+- **Console-like API**: Use `debug`, `info`, `warn`, and `error` methods.
3650 - **Plugin-based extensibility**: Extend functionality with plugins.
3751 - **Zero dependencies**: Lightweight and easy to integrate.
3852
39-## Examples
53+## Plugins
54+
55+Hellog's plugin system allows you to extend its functionality. Plugins can modify log messages and control how they are output.
56+
57+### Default Plugins
58+
59+Hellog comes with these plugins enabled by default:
4060
41-Basic logging:
61+- `HellogLineBreakDefaultPlugin` - A plugin to split `console.log` messages that would span over multiple lines and format each line separately.
62+- `HellogPrettyDefaultPlugin` - A plugin to format log messages in a more readable way: `{timestamp} [{level}] {message}`.
63+- `HellogStdoutDefaultPlugin` - A plugin to output log messages to stdout or stderr depending on the level.
4264
65+### Creating a Plugin
66+
67+Plugins are created by extending the `HellogPlugin` base class:
68+
69+```javascript
70+import { HellogPlugin } from '@steffthestunt/hellog';
71+
72+class TimestampPlugin extends HellogPlugin {
73+ transform(message, level) {
74+ const timestamp = new Date().toISOString();
75+ return `[${timestamp}] ${message}`;
76+ }
77+}
78+
79+// Initialize logger with custom plugins
80+const logger = new Hellog({
81+ plugins: [new TimestampPlugin()],
82+});
83+```
84+
85+### Plugin Examples
86+
87+Common plugin configurations:
88+
4389 ```javascript
44-hellog.log('This is a log message.');
45-hellog.info('This is an info message.');
46-hellog.warn('This is a warning message.');
47-hellog.error('This is an error message.');
90+import {
91+ HellogLineBreakPlugin,
92+ HellogPrettyPlugin,
93+ HellogStdoutPlugin,
94+} from '@steffthestunt/hellog';
95+
96+// Initialize with multiple plugins
97+const logger = new Hellog({
98+ plugins: [
99+ // Default plugins
100+ new HellogLineBreakPlugin(),
101+ new HellogPrettyPlugin(),
102+ new HellogStdoutPlugin(),
48103
49-// Accepts any input
50-hellog.log('Logging a number:', 123);
51-hellog.log('Logging an object:', { key: 'value' });
52-hellog.log('Logging an array:', [1, 2, 3]);
104+ // Custom plugins
105+ new TimestampPlugin(),
106+ ],
107+});
53108 ```