Skip to content

Commit

Permalink
Merge pull request #772 from jorenn92/refactor-logger-implement-winston
Browse files Browse the repository at this point in the history
feat: Implemented Winston logger for improved logging. Daily log rota…
  • Loading branch information
jorenn92 authored Jan 19, 2024
2 parents 0d23274 + 0e3ab51 commit f8fa98e
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 8 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.yarn
data/
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
data/maintainerr.sqlite
data/
docs-output/
node_modules
.yarn
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@
"@types/node": "^20.10.7",
"axios": "^1.6.5",
"bowser": "^2.11.0",
"chalk": "^4.1.2",
"cron": "3.1.3",
"cron-validator": "^1.3.1",
"crypto": "^1.0.1",
"http-server": "^14.1.1",
"lodash": "^4.17.21",
"nest-winston": "^1.9.4",
"next": "14.0.4",
"node-cache": "^5.1.2",
"path": "^0.12.7",
Expand All @@ -67,6 +69,8 @@
"typeorm": "^0.3.19",
"typescript": "^5.3.3",
"web-push": "^3.6.6",
"winston": "^3.11.0",
"winston-daily-rotate-file": "^4.7.1",
"xml2js": "^0.6.2",
"yaml": "^2.3.4"
},
Expand Down
63 changes: 59 additions & 4 deletions server/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,68 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app/app.module';
import { WinstonModule } from 'nest-winston';
import winston from 'winston';
import DailyRotateFile from 'winston-daily-rotate-file';
import chalk from 'chalk';

async function bootstrap() {
const app = await NestFactory.create(AppModule, {
logger:
process.env.NODE_ENV !== 'production' || process.env.DEBUG == 'true'
? ['log', 'debug', 'error', 'verbose', 'warn']
: ['error', 'warn', 'log'],
// Winston logger config
logger: WinstonModule.createLogger({
level:
process.env.NODE_ENV !== 'production' || process.env.DEBUG == 'true'
? 'silly'
: 'info',
format: winston.format.combine(
// winston.format.colorize(),
winston.format.timestamp({ format: 'DD/MM/YYYY HH:mm:ss' }),
// winston.format.timestamp(),
winston.format.printf(({ level, message, timestamp, context }) => {
const coloredTimestamp = chalk.white(timestamp);
level = `[${level.toUpperCase()}]`;
const coloredLevel =
level === '[DEBUG]'
? chalk.gray(level)
: level === '[ERROR]'
? chalk.red(level)
: level === '[WARN]'
? chalk.yellow(level)
: level === '[INFO]'
? chalk.green(level)
: chalk.cyan(level);

const coloredMessage =
level === '[DEBUG]'
? chalk.gray(message)
: level === '[ERROR]'
? chalk.red(message)
: level === '[WARN]'
? chalk.yellow(message)
: level === '[INFO]'
? chalk.green(message)
: chalk.cyan(message);
return `${chalk.green(`[maintainerr] |`)} ${coloredTimestamp} ${coloredLevel} ${chalk.blue(`[${context}]`)} ${coloredMessage}`;
}),
),
transports: [
new winston.transports.Console(),
new DailyRotateFile({
filename: '../data/logs/maintainerr-%DATE%.log',
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: '20m',
maxFiles: '7d',
format: winston.format.combine(
winston.format.timestamp({ format: 'DD/MM/YYYY HH:mm:ss' }), // winston.format.timestamp({ format: 'YYYY-MM-DDTHH:mm:ss.SSSZ' }),
winston.format.printf(({ level, message, timestamp, context }) => {
return `[maintainerr] | ${timestamp} [${level.toUpperCase()}] [${context}] ${message}`;
}),
),
}) as winston.transport,
],
}),
});
// End Winston logger config
app.enableCors();
await app.listen(3001);
}
Expand Down
Loading

0 comments on commit f8fa98e

Please sign in to comment.