Comments (4)
Description
I use the config on
bootstrap
function:new winston.transports.Console({ format: winston.format.combine( winston.format.timestamp(), winston.format.ms(), nestWinstonModuleUtilities.format.nestLike('MyApp', { colors: true, prettyPrint: true, }), ), }),when i print log, i get like:
[MyApp] Info 2023/8/3 23:11:33 {/test/easy-request, GET} - { trace_id: 'f95a2cd5ee3f1b39cc152bb6819c2547', span_id: '1837c6678bcb12bb', trace_flags: '01' } +6mCan someone tell me how to get the
trace_id
if i want use it?
First you need to create a middleware:
# logger.middleware.ts
import { createNamespace, getNamespace } from 'cls-hooked';
import { v4 as uuidv4 } from 'uuid';
export function requestIdMiddleware(req, res, next) {
const ns = createNamespace('request');
ns.run(() => {
ns.set('log_id', uuidv4());
next();
});
}
in main.ts add
...
const app = await NestFactory.create(AppModule, {
logger: WinstonModule.createLogger({
format: winston.format.combine(
winston.format.errors({ stack: true }),
winston.format.splat(),
winston.format.simple(),
winston.format.timestamp(),
winston.format.colorize(),
winston.format.printf((info) => {
const ns = getNamespace('request');
const log_id = ns ? ns.get('log_id') : null;
if (log_id) {
return `[${info.timestamp}] [${info.level}] [log_id: ${log_id}]: ${
info.message
} ${info?.stack || ''} ${info?.context || ''}`;
} else {
return `[${info.timestamp}] [${info.level}]: ${info.message} ${
info?.stack || ''
} ${info?.context || ''}`;
}
}),
),
transports: [
new winston.transports.Console(),
],
}),
});
app.use(requestIdMiddleware);
..
from nest-winston.
@crisgarlez I think it is overwrite default log, can i get original log trace_id ?
from nest-winston.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.
from nest-winston.
This issue was closed because it has been stalled for 7 days with no activity.
from nest-winston.
Related Issues (20)
- Nest can't resolve dependencies HOT 8
- Default context per service (configurable injection scope) HOT 3
- Passing winston logger instance to forRoot doesn't work HOT 2
- [Bug]: Creating multiple winston modules using child winston loggers use the last created winston module when using the nestjs Logger constructor HOT 2
- Adding "level" argument to log method HOT 2
- NestLike ConsoleFormat always contains stringifiedMeta and essentially logs message twice HOT 7
- LoggerService interface changed with Nest v8 HOT 2
- Is there a way to create a child logger? HOT 3
- How can I pass json attributes into the logger? HOT 3
- Support for @nestjs/common 10.0.0 in Peer Dependencies HOT 2
- [Question]: Should documentation be updated? HOT 8
- [Feature]: Support string-interpolation HOT 1
- [Feature]: add fatal level HOT 6
- [Question]: How can I access a request scope injectable when replacing the Nest logger (including boostrap) ? HOT 2
- [Question]: Winston creates file but does not save to it
- [Question]: processing optional arguments is not consistent with Nest HOT 4
- [Bug]: Redundant Stack Information in Error Logs HOT 2
- [Bug]: Code for v1.9.5 of Nest Winston not in NPM HOT 4
- [Bug]: Providing Logger from @nestjs/common as a provider in AppModule doesn't make it available globally to other modules/services. HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nest-winston.