Skip to content

Commit 857777b

Browse files
authored
Merge pull request #398 from trojs/feature/truncate-limit
Feature/truncate limit
2 parents 4b0cecc + c096ed1 commit 857777b

5 files changed

Lines changed: 706 additions & 527 deletions

File tree

README.md

Lines changed: 177 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# logger
2-
Generic logger with intergrations for e.g. Sentry
2+
3+
Generic logger with integrations for e.g. Sentry
4+
5+
## Features
6+
7+
* Multiple transport support (console, files, Sentry)
8+
* Winston-based logging with custom transports
9+
* Safe JSON serialization (handles circular references, deep objects, functions)
10+
* Stackdriver/Google Cloud Logging compatible
11+
* Automatic exception and rejection handling
12+
* Configurable log levels per transport
13+
* Production-ready error tracking with Sentry integration
14+
15+
## Quick Start
316

417
```javascript
518
import makeLogger from '@trojs/logger';
@@ -26,23 +39,36 @@ try {
2639
}
2740
```
2841

29-
# level
42+
## Configuration
43+
44+
### level
45+
46+
default: `info`
3047

31-
default: info
48+
Log only messages with a level less than or equal to this level. This acts as a global filter for all loggers unless a logger specifies its own level.
3249

33-
Log only if [`info.level`](#streams-objectmode-and-info-objects) less than or equal to this level
50+
Available levels (in order of priority):
51+
* `trace` (lowest)
52+
* `debug`
53+
* `info`
54+
* `warn`
55+
* `error`
56+
* `fatal` (highest)
3457

35-
More info see: https://www.npmjs.com/package/winston#logging-levels
58+
More info: <https://www.npmjs.com/package/winston#logging-levels>
3659

37-
# service
60+
### service
3861

39-
default: user-service
62+
default: `user-service`
4063

41-
# Loggers:
64+
The service name is used to identify the source of logs. This is particularly useful when aggregating logs from multiple services.
4265

43-
Set of logging targets for `info` messages
66+
## Loggers
67+
68+
Set of logging targets (transports) for log messages. Each logger can have its own configuration and log level.
69+
70+
Default configuration:
4471

45-
default:
4672
```javascript
4773
[
4874
{
@@ -51,17 +77,16 @@ default:
5177
]
5278
```
5379

54-
Types:
80+
Available logger types:
5581

56-
* sentry
57-
* errorFile
58-
* combinedFile
59-
* console
82+
* `console` - Logs to stdout/stderr
83+
* `errorFile` - Logs errors to a file
84+
* `combinedFile` - Logs all messages to a file
85+
* `sentry` - Sends errors to Sentry for tracking
6086

61-
The default loggers are overruled by the loggers in the `loggers` array.
87+
**Note:** The default loggers are replaced (not merged) when you provide a `loggers` array.
6288

63-
It use winston transports for all logger types.
64-
More info see: https://www.npmjs.com/package/winston#transports
89+
All loggers are implemented as Winston transports. More info: <https://www.npmjs.com/package/winston#transports>
6590

6691
## sentry
6792

@@ -71,30 +96,156 @@ More info see: https://www.npmjs.com/package/winston#transports
7196
* release (default: unknown, sentry.release)
7297
* debug (default: false, sentry.debug)
7398
* sampleRate (default: 1, sentry.sampleRate)
74-
* tracesSampleRate (default: 1, senty.tracesSampleRate)
99+
* tracesSampleRate (default: 1, sentry.tracesSampleRate)
75100
* level (default: info)
76101

77-
DSN:
102+
### DSN
78103

79104
The DSN tells the SDK where to send the events. If this value is not provided, the SDK will try to read it from the SENTRY_DSN environment variable. If that variable also does not exist, the SDK will just not send any events.
80105

81-
More info:
106+
### Example
82107

83-
* https://github.com/aandrewww/winston-transport-sentry-node
84-
* https://docs.sentry.io/platforms/node/
85-
* https://docs.sentry.io/platforms/javascript/
108+
```javascript
109+
const logger = makeLogger({
110+
loggers: [
111+
{
112+
type: 'sentry',
113+
location: 'https://12345678@234567151173.ingest.sentry.io/1234567',
114+
environment: 'production',
115+
release: 'v1.0.0',
116+
level: 'error'
117+
}
118+
]
119+
})
120+
```
121+
122+
More info:
123+
124+
* <https://github.com/aandrewww/winston-transport-sentry-node>
125+
* <https://docs.sentry.io/platforms/node/>
126+
* <https://docs.sentry.io/platforms/javascript/>
86127

87128
## errorFile
88129

89130
* location (default: error.log)
90131
* level (default: error)
91132

133+
Logs error-level messages to a file.
134+
135+
### Example
136+
137+
```javascript
138+
const logger = makeLogger({
139+
loggers: [
140+
{
141+
type: 'errorFile',
142+
location: './logs/error.log',
143+
level: 'error'
144+
}
145+
]
146+
})
147+
```
148+
92149
## combinedFile
93150

94151
* location (default: combined.log)
95152

153+
Logs all messages to a file regardless of level.
154+
155+
### Example
156+
157+
```javascript
158+
const logger = makeLogger({
159+
loggers: [
160+
{
161+
type: 'combinedFile',
162+
location: './logs/combined.log'
163+
}
164+
]
165+
})
166+
```
167+
96168
## console
97169

98170
* level (default: trace)
99-
* debug (default: false, stacktrace in console)
100-
* format (default: simple, also possible to set to json which is useful for different log systems)
171+
* debug (default: false, includes stacktrace in output)
172+
* format (default: simple, also accepts 'json' for structured logging systems)
173+
* maxDepth (default: 5, maximum depth for nested objects in JSON format only)
174+
* maxStringLength (default: 1000, maximum length for strings before truncation in JSON format only)
175+
176+
### JSON Format Features
177+
178+
When using `format: 'json'`, the console logger includes safe JSON serialization that handles:
179+
180+
* **Circular references**: Replaced with `[Circular]` to prevent serialization errors
181+
* **Deep objects**: Objects exceeding `maxDepth` are replaced with `[Max Depth Exceeded]`
182+
* **Long strings**: Strings exceeding `maxStringLength` are truncated with `... [truncated]`
183+
* **Functions**: Replaced with `[Function]` since they cannot be serialized
184+
* **Errors**: Automatically captures message, stack, and metadata
185+
* **Stackdriver format**: Compatible with Google Cloud Logging (includes severity, time, pid, hostname)
186+
187+
### Examples
188+
189+
Simple console logging:
190+
191+
```javascript
192+
const logger = makeLogger({
193+
loggers: [{ type: 'console' }]
194+
})
195+
```
196+
197+
JSON format with custom depth limits:
198+
199+
```javascript
200+
const logger = makeLogger({
201+
loggers: [
202+
{
203+
type: 'console',
204+
format: 'json',
205+
maxDepth: 3,
206+
maxStringLength: 500,
207+
debug: true
208+
}
209+
]
210+
})
211+
```
212+
213+
## Combining Multiple Loggers
214+
215+
You can use multiple loggers simultaneously with different configurations:
216+
217+
```javascript
218+
const logger = makeLogger({
219+
level: 'debug',
220+
service: 'my-api',
221+
loggers: [
222+
{
223+
type: 'console',
224+
format: 'json',
225+
level: 'debug'
226+
},
227+
{
228+
type: 'errorFile',
229+
location: './logs/error.log',
230+
level: 'error'
231+
},
232+
{
233+
type: 'combinedFile',
234+
location: './logs/combined.log'
235+
},
236+
{
237+
type: 'sentry',
238+
location: process.env.SENTRY_DSN,
239+
environment: process.env.NODE_ENV,
240+
level: 'error'
241+
}
242+
]
243+
})
244+
```
245+
246+
This configuration will:
247+
248+
* Log all debug+ messages to console in JSON format
249+
* Log only errors to `error.log`
250+
* Log all messages to `combined.log`
251+
* Send only errors to Sentry

0 commit comments

Comments
 (0)