-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(winston): integrate winston logger
add @kibibit/nestjs-winston to support logging with winston, and a mock service for the test to disable logging alltogether for the test applications
- Loading branch information
1 parent
4e2817a
commit f3793d5
Showing
10 changed files
with
402 additions
and
41 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
import winston, { createLogger } from 'winston'; | ||
|
||
import { | ||
winstonInstance | ||
} from '@kibibit/nestjs-winston'; | ||
|
||
winstonInstance.logger = createLogger({ | ||
transports: [ | ||
new winston.transports.Console({ | ||
silent: true, | ||
level: 'debug' | ||
}) | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { basename } from 'path'; | ||
|
||
import bytes from 'bytes'; | ||
import winston, { createLogger } from 'winston'; | ||
|
||
import { | ||
utilities as nestWinstonModuleUtilities, | ||
winstonInstance | ||
} from '@kibibit/nestjs-winston'; | ||
|
||
const fiveMegaBytes = bytes('5MB'); | ||
|
||
const omitMeta = [ | ||
'file', | ||
'env' | ||
]; | ||
|
||
// console.log(Intl.DateTimeFormat().resolvedOptions().timeZone); | ||
|
||
winstonInstance.logger = createLogger({ | ||
transports: [ | ||
new winston.transports.Console({ | ||
level: 'debug', | ||
format: winston.format.combine( | ||
winston.format.timestamp(), | ||
winston.format.ms(), | ||
nestWinstonModuleUtilities.format.nestLike('achievibit', omitMeta), | ||
) | ||
}), | ||
new winston.transports.File({ | ||
level: 'debug', | ||
filename: '../logs/server.log', | ||
maxsize: fiveMegaBytes, | ||
maxFiles: 5, | ||
tailable: true | ||
}) | ||
], | ||
exceptionHandlers: [ | ||
new winston.transports.File({ | ||
level: 'debug', | ||
filename: '../logs/exceptions.log', | ||
maxsize: fiveMegaBytes, | ||
maxFiles: 5, | ||
tailable: true | ||
}) | ||
], | ||
handleExceptions: true, | ||
format: winston.format.combine( | ||
winston.format((info) => { | ||
info.env = process.env.NODE_ENV; | ||
const filename = getCallerFile(); | ||
|
||
if (filename) { | ||
info.file = basename(getCallerFile()); | ||
} | ||
return info; | ||
})(), | ||
winston.format.timestamp(), | ||
winston.format.splat(), | ||
winston.format.json() | ||
) | ||
}); | ||
|
||
function getCallerFile(): string { | ||
try { | ||
const err = new Error(); | ||
let callerfile; | ||
Error.prepareStackTrace = function (err, stack) { return stack; }; | ||
const currentfile = (err.stack as any).shift().getFileName(); | ||
|
||
while (err.stack.length) { | ||
callerfile = (err.stack as any).shift().getFileName(); | ||
|
||
if (currentfile !== callerfile && | ||
!callerfile.includes('node_modules') && | ||
!callerfile.includes('internal/process')) return callerfile; | ||
} | ||
} catch (err) { } | ||
return ''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters