@decaf-ts/logging 0.2.3 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -157
- package/README.md +363 -2
- package/dist/logging.cjs +304 -89
- package/dist/logging.esm.cjs +304 -89
- package/lib/constants.cjs +28 -15
- package/lib/constants.d.ts +26 -14
- package/lib/decorators.cjs +65 -1
- package/lib/decorators.d.ts +64 -0
- package/lib/esm/constants.d.ts +26 -14
- package/lib/esm/constants.js +28 -15
- package/lib/esm/decorators.d.ts +64 -0
- package/lib/esm/decorators.js +67 -3
- package/lib/esm/index.d.ts +6 -12
- package/lib/esm/index.js +11 -17
- package/lib/esm/logging.d.ts +190 -63
- package/lib/esm/logging.js +208 -64
- package/lib/esm/types.d.ts +57 -9
- package/lib/esm/types.js +1 -1
- package/lib/esm/winston/winston.d.ts +39 -0
- package/lib/esm/winston/winston.js +41 -2
- package/lib/index.cjs +7 -13
- package/lib/index.d.ts +6 -12
- package/lib/logging.cjs +207 -63
- package/lib/logging.d.ts +190 -63
- package/lib/types.cjs +1 -1
- package/lib/types.d.ts +57 -9
- package/lib/winston/winston.cjs +40 -1
- package/lib/winston/winston.d.ts +39 -0
- package/package.json +2 -2
package/LICENSE.md
CHANGED
|
@@ -1,157 +1,21 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
than an Application or a Combined Work as defined below.
|
|
23
|
-
|
|
24
|
-
An "Application" is any work that makes use of an interface provided
|
|
25
|
-
by the Library, but which is not otherwise based on the Library.
|
|
26
|
-
Defining a subclass of a class defined by the Library is deemed a mode
|
|
27
|
-
of using an interface provided by the Library.
|
|
28
|
-
|
|
29
|
-
A "Combined Work" is a work produced by combining or linking an
|
|
30
|
-
Application with the Library. The particular version of the Library
|
|
31
|
-
with which the Combined Work was made is also called the "Linked
|
|
32
|
-
Version".
|
|
33
|
-
|
|
34
|
-
The "Minimal Corresponding Source" for a Combined Work means the
|
|
35
|
-
Corresponding Source for the Combined Work, excluding any source code
|
|
36
|
-
for portions of the Combined Work that, considered in isolation, are
|
|
37
|
-
based on the Application, and not on the Linked Version.
|
|
38
|
-
|
|
39
|
-
The "Corresponding Application Code" for a Combined Work means the
|
|
40
|
-
object code and/or source code for the Application, including any data
|
|
41
|
-
and utility programs needed for reproducing the Combined Work from the
|
|
42
|
-
Application, but excluding the System Libraries of the Combined Work.
|
|
43
|
-
|
|
44
|
-
## 1. Exception to Section 3 of the GNU GPL.
|
|
45
|
-
|
|
46
|
-
You may convey a covered work under sections 3 and 4 of this License
|
|
47
|
-
without being bound by section 3 of the GNU GPL.
|
|
48
|
-
|
|
49
|
-
## 2. Conveying Modified Versions.
|
|
50
|
-
|
|
51
|
-
If you modify a copy of the Library, and, in your modifications, a
|
|
52
|
-
facility refers to a function or data to be supplied by an Application
|
|
53
|
-
that uses the facility (other than as an argument passed when the
|
|
54
|
-
facility is invoked), then you may convey a copy of the modified
|
|
55
|
-
version:
|
|
56
|
-
|
|
57
|
-
- a) under this License, provided that you make a good faith effort
|
|
58
|
-
to ensure that, in the event an Application does not supply the
|
|
59
|
-
function or data, the facility still operates, and performs
|
|
60
|
-
whatever part of its purpose remains meaningful, or
|
|
61
|
-
- b) under the GNU GPL, with none of the additional permissions of
|
|
62
|
-
this License applicable to that copy.
|
|
63
|
-
|
|
64
|
-
## 3. Object Code Incorporating Material from Library Header Files.
|
|
65
|
-
|
|
66
|
-
The object code form of an Application may incorporate material from a
|
|
67
|
-
header file that is part of the Library. You may convey such object
|
|
68
|
-
code under terms of your choice, provided that, if the incorporated
|
|
69
|
-
material is not limited to numerical parameters, data structure
|
|
70
|
-
layouts and accessors, or small macros, inline functions and templates
|
|
71
|
-
(ten or fewer lines in length), you do both of the following:
|
|
72
|
-
|
|
73
|
-
- a) Give prominent notice with each copy of the object code that
|
|
74
|
-
the Library is used in it and that the Library and its use are
|
|
75
|
-
covered by this License.
|
|
76
|
-
- b) Accompany the object code with a copy of the GNU GPL and this
|
|
77
|
-
license document.
|
|
78
|
-
|
|
79
|
-
## 4. Combined Works.
|
|
80
|
-
|
|
81
|
-
You may convey a Combined Work under terms of your choice that, taken
|
|
82
|
-
together, effectively do not restrict modification of the portions of
|
|
83
|
-
the Library contained in the Combined Work and reverse engineering for
|
|
84
|
-
debugging such modifications, if you also do each of the following:
|
|
85
|
-
|
|
86
|
-
- a) Give prominent notice with each copy of the Combined Work that
|
|
87
|
-
the Library is used in it and that the Library and its use are
|
|
88
|
-
covered by this License.
|
|
89
|
-
- b) Accompany the Combined Work with a copy of the GNU GPL and this
|
|
90
|
-
license document.
|
|
91
|
-
- c) For a Combined Work that displays copyright notices during
|
|
92
|
-
execution, include the copyright notice for the Library among
|
|
93
|
-
these notices, as well as a reference directing the user to the
|
|
94
|
-
copies of the GNU GPL and this license document.
|
|
95
|
-
- d) Do one of the following:
|
|
96
|
-
- 0) Convey the Minimal Corresponding Source under the terms of
|
|
97
|
-
this License, and the Corresponding Application Code in a form
|
|
98
|
-
suitable for, and under terms that permit, the user to
|
|
99
|
-
recombine or relink the Application with a modified version of
|
|
100
|
-
the Linked Version to produce a modified Combined Work, in the
|
|
101
|
-
manner specified by section 6 of the GNU GPL for conveying
|
|
102
|
-
Corresponding Source.
|
|
103
|
-
- 1) Use a suitable shared library mechanism for linking with
|
|
104
|
-
the Library. A suitable mechanism is one that (a) uses at run
|
|
105
|
-
time a copy of the Library already present on the user's
|
|
106
|
-
computer system, and (b) will operate properly with a modified
|
|
107
|
-
version of the Library that is interface-compatible with the
|
|
108
|
-
Linked Version.
|
|
109
|
-
- e) Provide Installation Information, but only if you would
|
|
110
|
-
otherwise be required to provide such information under section 6
|
|
111
|
-
of the GNU GPL, and only to the extent that such information is
|
|
112
|
-
necessary to install and execute a modified version of the
|
|
113
|
-
Combined Work produced by recombining or relinking the Application
|
|
114
|
-
with a modified version of the Linked Version. (If you use option
|
|
115
|
-
4d0, the Installation Information must accompany the Minimal
|
|
116
|
-
Corresponding Source and Corresponding Application Code. If you
|
|
117
|
-
use option 4d1, you must provide the Installation Information in
|
|
118
|
-
the manner specified by section 6 of the GNU GPL for conveying
|
|
119
|
-
Corresponding Source.)
|
|
120
|
-
|
|
121
|
-
## 5. Combined Libraries.
|
|
122
|
-
|
|
123
|
-
You may place library facilities that are a work based on the Library
|
|
124
|
-
side by side in a single library together with other library
|
|
125
|
-
facilities that are not Applications and are not covered by this
|
|
126
|
-
License, and convey such a combined library under terms of your
|
|
127
|
-
choice, if you do both of the following:
|
|
128
|
-
|
|
129
|
-
- a) Accompany the combined library with a copy of the same work
|
|
130
|
-
based on the Library, uncombined with any other library
|
|
131
|
-
facilities, conveyed under the terms of this License.
|
|
132
|
-
- b) Give prominent notice with the combined library that part of it
|
|
133
|
-
is a work based on the Library, and explaining where to find the
|
|
134
|
-
accompanying uncombined form of the same work.
|
|
135
|
-
|
|
136
|
-
## 6. Revised Versions of the GNU Lesser General Public License.
|
|
137
|
-
|
|
138
|
-
The Free Software Foundation may publish revised and/or new versions
|
|
139
|
-
of the GNU Lesser General Public License from time to time. Such new
|
|
140
|
-
versions will be similar in spirit to the present version, but may
|
|
141
|
-
differ in detail to address new problems or concerns.
|
|
142
|
-
|
|
143
|
-
Each version is given a distinguishing version number. If the Library
|
|
144
|
-
as you received it specifies that a certain numbered version of the
|
|
145
|
-
GNU Lesser General Public License "or any later version" applies to
|
|
146
|
-
it, you have the option of following the terms and conditions either
|
|
147
|
-
of that published version or of any later version published by the
|
|
148
|
-
Free Software Foundation. If the Library as you received it does not
|
|
149
|
-
specify a version number of the GNU Lesser General Public License, you
|
|
150
|
-
may choose any version of the GNU Lesser General Public License ever
|
|
151
|
-
published by the Free Software Foundation.
|
|
152
|
-
|
|
153
|
-
If the Library as you received it specifies that a proxy can decide
|
|
154
|
-
whether future versions of the GNU Lesser General Public License shall
|
|
155
|
-
apply, that proxy's public statement of acceptance of any version is
|
|
156
|
-
permanent authorization for you to choose that version for the
|
|
157
|
-
Library.
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Tiago Venceslau
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Decaf's Logging
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
A comprehensive, flexible, and type-safe logging library for TypeScript applications that provides hierarchical context-aware logging, configurable styling, multiple output formats, and method decorators. It offers a lightweight built-in logger and seamless Winston integration, enabling developers to easily add structured logging with different verbosity levels to their applications.
|
|
7
6
|
|
|
8
7
|
|
|
9
8
|

|
|
@@ -37,6 +36,83 @@ Documentation available [here](https://decaf-ts.github.io/logging/)
|
|
|
37
36
|
|
|
38
37
|
### Description
|
|
39
38
|
|
|
39
|
+
Decaf's Logging is a powerful TypeScript logging library designed to provide flexible, context-aware logging capabilities for applications of any size. The library is built with a focus on type safety, configurability, and ease of use.
|
|
40
|
+
|
|
41
|
+
#### Core Architecture
|
|
42
|
+
|
|
43
|
+
The library follows a modular architecture with several key components:
|
|
44
|
+
|
|
45
|
+
1. **Logging System**:
|
|
46
|
+
- `Logging`: A static class that serves as the central entry point for the logging system. It manages global configuration, provides factory methods for creating loggers, and offers static logging methods.
|
|
47
|
+
- `MiniLogger`: A lightweight logger implementation that provides the core logging functionality with support for different log levels, context-aware logging, and customizable formatting.
|
|
48
|
+
- `Logger` interface: Defines the standard methods that all logger implementations must provide, ensuring consistency across different logger types.
|
|
49
|
+
|
|
50
|
+
2. **Configuration System**:
|
|
51
|
+
- `LoggingConfig`: Defines configuration options for the logging system, including log level, verbosity, styling, timestamp format, and more.
|
|
52
|
+
- `DefaultLoggingConfig`: Provides sensible default settings that can be overridden as needed.
|
|
53
|
+
|
|
54
|
+
3. **Log Levels**:
|
|
55
|
+
- `LogLevel` enum: Defines standard log levels (error, info, verbose, debug, silly) for categorizing log messages.
|
|
56
|
+
- `NumericLogLevels`: Maps log levels to numeric values for comparison and filtering.
|
|
57
|
+
|
|
58
|
+
4. **Styling System**:
|
|
59
|
+
- `Theme` interface: Defines a comprehensive theming system for styling log output with colors and formatting.
|
|
60
|
+
- `DefaultTheme`: Provides a default color scheme for log output.
|
|
61
|
+
- `LoggingMode` enum: Supports different output formats (RAW, JSON) for log messages.
|
|
62
|
+
|
|
63
|
+
5. **Decorator System**:
|
|
64
|
+
- Method decorators (`@log`, `@debug`, `@info`, `@verbose`, `@silly`): Allow for easy integration of logging into class methods with options for benchmarking and verbosity control.
|
|
65
|
+
|
|
66
|
+
6. **Winston Integration**:
|
|
67
|
+
- `WinstonLogger`: Extends the core logging functionality to leverage the Winston logging library.
|
|
68
|
+
- `WinstonFactory`: A factory function for creating Winston-based loggers.
|
|
69
|
+
|
|
70
|
+
#### Key Features
|
|
71
|
+
|
|
72
|
+
1. **Hierarchical Context-Aware Logging**:
|
|
73
|
+
The library allows creating loggers for specific classes and methods, maintaining a hierarchy of contexts. This makes it easy to trace log messages back to their source and filter logs by context.
|
|
74
|
+
|
|
75
|
+
2. **Configurable Styling**:
|
|
76
|
+
Extensive support for styling log output with colors and formatting, with a theme system that allows customizing the appearance of different log components.
|
|
77
|
+
|
|
78
|
+
3. **Multiple Output Formats**:
|
|
79
|
+
Support for both human-readable (RAW) and machine-parseable (JSON) output formats.
|
|
80
|
+
|
|
81
|
+
4. **Method Decorators**:
|
|
82
|
+
Easy-to-use decorators for adding logging to class methods, with support for benchmarking execution time.
|
|
83
|
+
|
|
84
|
+
5. **Verbosity Control**:
|
|
85
|
+
Fine-grained control over log verbosity, allowing developers to adjust the detail level of logs without changing code.
|
|
86
|
+
|
|
87
|
+
6. **Type Safety**:
|
|
88
|
+
Comprehensive TypeScript type definitions ensure type safety and enable IDE autocompletion.
|
|
89
|
+
|
|
90
|
+
7. **Winston Integration**:
|
|
91
|
+
Seamless integration with the popular Winston logging library, providing access to its advanced features while maintaining the same interface.
|
|
92
|
+
|
|
93
|
+
8. **Error Handling**:
|
|
94
|
+
Special support for logging errors with stack traces for better debugging.
|
|
95
|
+
|
|
96
|
+
#### Usage Patterns
|
|
97
|
+
|
|
98
|
+
The library supports several usage patterns:
|
|
99
|
+
|
|
100
|
+
1. **Global Logging**:
|
|
101
|
+
Using the static `Logging` class methods for simple, application-wide logging.
|
|
102
|
+
|
|
103
|
+
2. **Class-Specific Logging**:
|
|
104
|
+
Creating loggers for specific classes to provide context for log messages.
|
|
105
|
+
|
|
106
|
+
3. **Method-Specific Logging**:
|
|
107
|
+
Creating child loggers for specific methods to further refine the context.
|
|
108
|
+
|
|
109
|
+
4. **Decorator-Based Logging**:
|
|
110
|
+
Using method decorators to automatically log method calls and execution times.
|
|
111
|
+
|
|
112
|
+
5. **Winston-Based Logging**:
|
|
113
|
+
Leveraging Winston's advanced features while maintaining the same interface.
|
|
114
|
+
|
|
115
|
+
This flexible design makes the library suitable for a wide range of applications, from simple scripts to complex enterprise systems.
|
|
40
116
|
|
|
41
117
|
|
|
42
118
|
### How to Use
|
|
@@ -44,7 +120,292 @@ Documentation available [here](https://decaf-ts.github.io/logging/)
|
|
|
44
120
|
- [Initial Setup](./tutorials/For%20Developers.md#_initial-setup_)
|
|
45
121
|
- [Installation](./tutorials/For%20Developers.md#installation)
|
|
46
122
|
|
|
123
|
+
## Basic Usage
|
|
124
|
+
|
|
125
|
+
### Global Logging
|
|
126
|
+
|
|
127
|
+
The simplest way to use the library is through the static `Logging` class:
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
import { Logging, LogLevel } from 'decaf-logging';
|
|
131
|
+
|
|
132
|
+
// Configure global logging settings
|
|
133
|
+
Logging.setConfig({
|
|
134
|
+
level: LogLevel.debug,
|
|
135
|
+
style: true,
|
|
136
|
+
timestamp: true
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// Log messages at different levels
|
|
140
|
+
Logging.info('Application started');
|
|
141
|
+
Logging.debug('Debug information');
|
|
142
|
+
Logging.error('An error occurred');
|
|
143
|
+
Logging.verbose('Detailed information', 1); // With verbosity level
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Class-Specific Logging
|
|
147
|
+
|
|
148
|
+
For more context-aware logging, create loggers for specific classes:
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
import { Logging, Logger } from 'decaf-logging';
|
|
152
|
+
|
|
153
|
+
class UserService {
|
|
154
|
+
private logger: Logger;
|
|
155
|
+
|
|
156
|
+
constructor() {
|
|
157
|
+
// Create a logger for this class
|
|
158
|
+
this.logger = Logging.for(UserService);
|
|
159
|
+
// Or with string name: Logging.for('UserService');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
getUser(id: string) {
|
|
163
|
+
this.logger.info(`Getting user with ID: ${id}`);
|
|
164
|
+
// ... implementation
|
|
165
|
+
this.logger.debug('User retrieved successfully');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
updateUser(id: string, data: any) {
|
|
169
|
+
try {
|
|
170
|
+
this.logger.info(`Updating user with ID: ${id}`);
|
|
171
|
+
// ... implementation
|
|
172
|
+
this.logger.debug('User updated successfully');
|
|
173
|
+
} catch (error) {
|
|
174
|
+
this.logger.error(error); // Logs error with stack trace
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### Method-Specific Logging
|
|
181
|
+
|
|
182
|
+
Create child loggers for specific methods to further refine the context:
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
import { Logging, Logger } from 'decaf-logging';
|
|
47
186
|
|
|
187
|
+
class DataProcessor {
|
|
188
|
+
private logger: Logger;
|
|
189
|
+
|
|
190
|
+
constructor() {
|
|
191
|
+
this.logger = Logging.for(DataProcessor);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
processData(data: any[]) {
|
|
195
|
+
// Create a method-specific logger
|
|
196
|
+
const methodLogger = this.logger.for('processData');
|
|
197
|
+
|
|
198
|
+
methodLogger.info(`Processing ${data.length} items`);
|
|
199
|
+
|
|
200
|
+
// With custom configuration
|
|
201
|
+
const verboseLogger = methodLogger.for('details', { verbose: 2 });
|
|
202
|
+
verboseLogger.verbose('Starting detailed processing', 1);
|
|
203
|
+
|
|
204
|
+
// ... implementation
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Using Decorators
|
|
210
|
+
|
|
211
|
+
Decorators provide an easy way to add logging to class methods:
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
import { debug, info, log, LogLevel, verbose } from 'decaf-logging';
|
|
215
|
+
|
|
216
|
+
class PaymentProcessor {
|
|
217
|
+
// Basic logging with info level
|
|
218
|
+
@info()
|
|
219
|
+
processPayment(amount: number, userId: string) {
|
|
220
|
+
// Method implementation
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Debug level logging with benchmarking
|
|
225
|
+
@debug(true)
|
|
226
|
+
validatePayment(paymentData: any) {
|
|
227
|
+
// Method implementation
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Verbose logging with custom verbosity
|
|
232
|
+
@verbose(2)
|
|
233
|
+
recordTransaction(transactionData: any) {
|
|
234
|
+
// Method implementation
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Custom log level with benchmarking and verbosity
|
|
238
|
+
@log(LogLevel.error, true, 1)
|
|
239
|
+
handleFailure(error: Error) {
|
|
240
|
+
// Method implementation
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Winston Integration
|
|
246
|
+
|
|
247
|
+
To use Winston for logging:
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
import { Logging, LogLevel } from 'decaf-logging';
|
|
251
|
+
import { WinstonFactory } from 'decaf-logging/winston';
|
|
252
|
+
import winston from 'winston';
|
|
253
|
+
|
|
254
|
+
// Configure Winston transports
|
|
255
|
+
const transports = [
|
|
256
|
+
new winston.transports.Console(),
|
|
257
|
+
new winston.transports.File({ filename: 'app.log' })
|
|
258
|
+
];
|
|
259
|
+
|
|
260
|
+
// Set Winston as the logger factory
|
|
261
|
+
Logging.setFactory(WinstonFactory);
|
|
262
|
+
|
|
263
|
+
// Configure global logging
|
|
264
|
+
Logging.setConfig({
|
|
265
|
+
level: LogLevel.info,
|
|
266
|
+
timestamp: true,
|
|
267
|
+
timestampFormat: 'YYYY-MM-DD HH:mm:ss'
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
// Create a Winston logger for a class
|
|
271
|
+
const logger = Logging.for('MyService');
|
|
272
|
+
logger.info('Service initialized');
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## Advanced Usage
|
|
276
|
+
|
|
277
|
+
### Custom Styling
|
|
278
|
+
|
|
279
|
+
Configure custom styling for log output:
|
|
280
|
+
|
|
281
|
+
```typescript
|
|
282
|
+
import { Logging, LogLevel, Theme } from 'decaf-logging';
|
|
283
|
+
|
|
284
|
+
// Define a custom theme
|
|
285
|
+
const customTheme: Theme = {
|
|
286
|
+
class: {
|
|
287
|
+
fg: 35, // Magenta
|
|
288
|
+
},
|
|
289
|
+
id: {
|
|
290
|
+
fg: 36, // Cyan
|
|
291
|
+
},
|
|
292
|
+
stack: {},
|
|
293
|
+
timestamp: {
|
|
294
|
+
fg: 90, // Gray
|
|
295
|
+
},
|
|
296
|
+
message: {
|
|
297
|
+
error: {
|
|
298
|
+
fg: 31, // Red
|
|
299
|
+
style: ['bold'],
|
|
300
|
+
},
|
|
301
|
+
},
|
|
302
|
+
method: {},
|
|
303
|
+
logLevel: {
|
|
304
|
+
error: {
|
|
305
|
+
fg: 31, // Red
|
|
306
|
+
style: ['bold'],
|
|
307
|
+
},
|
|
308
|
+
info: {
|
|
309
|
+
fg: 32, // Green
|
|
310
|
+
},
|
|
311
|
+
verbose: {
|
|
312
|
+
fg: 34, // Blue
|
|
313
|
+
},
|
|
314
|
+
debug: {
|
|
315
|
+
fg: 33, // Yellow
|
|
316
|
+
},
|
|
317
|
+
},
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
// Apply the custom theme
|
|
321
|
+
Logging.setConfig({
|
|
322
|
+
style: true,
|
|
323
|
+
theme: customTheme
|
|
324
|
+
});
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
### Correlation IDs
|
|
328
|
+
|
|
329
|
+
Track related log messages with correlation IDs:
|
|
330
|
+
|
|
331
|
+
```typescript
|
|
332
|
+
import { Logging } from 'decaf-logging';
|
|
333
|
+
|
|
334
|
+
function processRequest(requestId: string, data: any) {
|
|
335
|
+
// Create a logger with correlation ID
|
|
336
|
+
const logger = Logging.for('RequestProcessor', {
|
|
337
|
+
correlationId: requestId
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
logger.info('Processing request');
|
|
341
|
+
|
|
342
|
+
// All log messages from this logger will include the correlation ID
|
|
343
|
+
processRequestData(data, logger);
|
|
344
|
+
|
|
345
|
+
logger.info('Request processing completed');
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function processRequestData(data: any, logger: Logger) {
|
|
349
|
+
// Child loggers inherit the correlation ID
|
|
350
|
+
const dataLogger = logger.for('DataProcessor');
|
|
351
|
+
dataLogger.debug('Processing data');
|
|
352
|
+
// ...
|
|
353
|
+
}
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
### JSON Output Mode
|
|
357
|
+
|
|
358
|
+
For machine-readable logs:
|
|
359
|
+
|
|
360
|
+
```typescript
|
|
361
|
+
import { Logging, LoggingMode } from 'decaf-logging';
|
|
362
|
+
|
|
363
|
+
// Configure for JSON output
|
|
364
|
+
Logging.setConfig({
|
|
365
|
+
mode: LoggingMode.JSON,
|
|
366
|
+
timestamp: true
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
Logging.info('This will be output in JSON format');
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
### Custom Logger Factory
|
|
373
|
+
|
|
374
|
+
Create a custom logger implementation:
|
|
375
|
+
|
|
376
|
+
```typescript
|
|
377
|
+
import { Logger, LoggerFactory, Logging, MiniLogger } from 'decaf-logging';
|
|
378
|
+
|
|
379
|
+
// Custom logger that adds prefix to all messages
|
|
380
|
+
class PrefixedLogger extends MiniLogger {
|
|
381
|
+
constructor(context: string, prefix: string, conf?: Partial<LoggingConfig>) {
|
|
382
|
+
super(context, conf);
|
|
383
|
+
this.prefix = prefix;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
private prefix: string;
|
|
387
|
+
|
|
388
|
+
protected override createLog(level: LogLevel, message: StringLike | Error, stack?: string): string {
|
|
389
|
+
const msg = typeof message === 'string' ? message : message.message;
|
|
390
|
+
const prefixedMsg = `${this.prefix}: ${msg}`;
|
|
391
|
+
return super.createLog(level, prefixedMsg, stack);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Custom factory function
|
|
396
|
+
const PrefixedLoggerFactory: LoggerFactory = (
|
|
397
|
+
context: string,
|
|
398
|
+
conf?: Partial<LoggingConfig>,
|
|
399
|
+
...args: any[]
|
|
400
|
+
) => new PrefixedLogger(context, args[0] || '[APP]', conf);
|
|
401
|
+
|
|
402
|
+
// Set the custom factory
|
|
403
|
+
Logging.setFactory(PrefixedLoggerFactory);
|
|
404
|
+
|
|
405
|
+
// Create a logger with the custom factory
|
|
406
|
+
const logger = Logging.for('MyClass', undefined, '[CUSTOM]');
|
|
407
|
+
logger.info('Hello world'); // Outputs: "[CUSTOM]: Hello world"
|
|
408
|
+
```
|
|
48
409
|
|
|
49
410
|
|
|
50
411
|
### Related
|