@trojs/logger 2.4.0 → 2.5.1

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@trojs/logger",
3
3
  "description": "Winston logger for TroJS",
4
- "version": "2.4.0",
4
+ "version": "2.5.1",
5
5
  "author": {
6
6
  "name": "Pieter Wigboldus",
7
7
  "url": "https://trojs.org/"
package/src/logger.js CHANGED
@@ -29,6 +29,7 @@ const levels = {
29
29
  * @param {Array<{[key: string]: string}>} [options.loggers=defaultLoggers] - Array of logger transport configurations.
30
30
  * @param {string} [options.level='info'] - Minimum log level for the logger.
31
31
  * @param {object} [options.meta={}] - Default metadata to include in all log messages.
32
+ * @param {boolean} [options.exitOnError=false] - Whether the logger should exit on error.
32
33
  * @returns {LoggerType} Winston logger instance with custom level wrappers.
33
34
  * These handlers will log errors and warnings using the logger, and are only attached once per process.
34
35
  * @example
@@ -36,14 +37,15 @@ const levels = {
36
37
  * const logger = createLogger({ level: 'debug', meta: { service: 'api' } });
37
38
  * logger.info('Service started');
38
39
  */
39
- export default ({ loggers = defaultLoggers, level = 'info', meta = {} } = {}) => {
40
+ export default ({ loggers = defaultLoggers, level = 'info', meta = {}, exitOnError = false } = {}) => {
40
41
  const winstonLoggers = makeLoggers({ winston, loggers })
41
42
 
42
43
  const logger = winston.createLogger({
43
44
  level,
44
45
  levels,
45
46
  defaultMeta: meta,
46
- transports: winstonLoggers
47
+ transports: winstonLoggers,
48
+ exitOnError
47
49
  })
48
50
 
49
51
  const wrapLevel = (lvl) => {
@@ -81,39 +83,14 @@ export default ({ loggers = defaultLoggers, level = 'info', meta = {} } = {}) =>
81
83
  if (typeof logger[lvl] === 'function') wrapLevel(lvl)
82
84
  })
83
85
 
86
+ // Winston handles uncaughtException and unhandledRejection via transport
87
+ // handleExceptions and handleRejections options.
88
+ // exitOnError controls whether the process exits after logging.
89
+
90
+ // Only attach warning handler (not handled by Winston transports)
84
91
  if (!process.__trojsLoggerHandlersAttached) {
85
92
  process.__trojsLoggerHandlersAttached = true
86
93
 
87
- process.on('uncaughtException', (err) => {
88
- try {
89
- logger.error(err instanceof Error ? err : new Error(String(err)))
90
- } catch {
91
- // eslint-disable-next-line no-console
92
- console.error('UNCAUGHT_EXCEPTION', err)
93
- }
94
- })
95
-
96
- process.on('unhandledRejection', (reason) => {
97
- let err
98
- if (reason instanceof Error) {
99
- err = reason
100
- } else if (typeof reason === 'string') {
101
- err = new Error(reason)
102
- } else {
103
- try {
104
- err = new Error(JSON.stringify(reason))
105
- } catch {
106
- err = new Error(String(reason))
107
- }
108
- }
109
- try {
110
- logger.error(err)
111
- } catch {
112
- // eslint-disable-next-line no-console
113
- console.error('UNHANDLED_REJECTION', err)
114
- }
115
- })
116
-
117
94
  process.on('warning', (warning) => {
118
95
  try {
119
96
  logger.warn(
@@ -1,4 +1,6 @@
1
1
  export default ({ winston, logger }) =>
2
2
  new winston.transports.File({
3
- filename: logger?.location || 'combined.log'
3
+ filename: logger?.location || 'combined.log',
4
+ handleExceptions: true,
5
+ handleRejections: true
4
6
  })
@@ -1,5 +1,7 @@
1
1
  export default ({ winston, logger }) =>
2
2
  new winston.transports.File({
3
3
  filename: logger?.location || 'error.log',
4
- level: logger?.level || 'error'
4
+ level: logger?.level || 'error',
5
+ handleExceptions: true,
6
+ handleRejections: true
5
7
  })
@@ -11,7 +11,9 @@ export default ({ logger }) => {
11
11
  sampleRate: logger?.sampleRate || 1,
12
12
  tracesSampleRate: logger?.tracesSampleRate || 1
13
13
  },
14
- level: logger?.level || 'info'
14
+ level: logger?.level || 'info',
15
+ handleExceptions: true,
16
+ handleRejections: true
15
17
  }
16
18
 
17
19
  return new SentryTransport(options)