chore: added Winston
This commit is contained in:
@@ -4,6 +4,7 @@ import express, { Request, Response } from 'express';
|
||||
import helmet from 'helmet';
|
||||
import { Validator } from 'jsonschema';
|
||||
import { addition } from '../utils/addition';
|
||||
import { logger } from '../utils/logger';
|
||||
|
||||
const server = express();
|
||||
server.use(cors());
|
||||
@@ -22,6 +23,7 @@ const schema = {
|
||||
};
|
||||
|
||||
server.post('/', (req: Request, res: Response) => {
|
||||
logger.info(`POST / with ${JSON.stringify(req.body)}`);
|
||||
if (!validator.validate(req.body, schema).valid) {
|
||||
return res.status(400).json({ message: 'Malformed query parameters' });
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { logger } from './logger';
|
||||
|
||||
/**
|
||||
* Format a response message containing a user input.
|
||||
* @param number - The user input value.
|
||||
* @returns The value increased by one.
|
||||
*/
|
||||
export function addition(value: number) {
|
||||
logger.info(`addition(${value})`);
|
||||
return value + 1;
|
||||
}
|
||||
|
||||
21
src/utils/logger.ts
Normal file
21
src/utils/logger.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import winston from 'winston';
|
||||
import 'winston-daily-rotate-file';
|
||||
const { combine, timestamp, printf, align } = winston.format;
|
||||
|
||||
const fileRotateTransport = new winston.transports.DailyRotateFile({
|
||||
filename: 'logs/winston.log',
|
||||
datePattern: 'YYYY-MM-DD',
|
||||
maxFiles: '7d'
|
||||
});
|
||||
|
||||
export const logger = winston.createLogger({
|
||||
level: 'info',
|
||||
format: combine(
|
||||
timestamp({
|
||||
format: 'YYYY-MM-DD hh:mm:ss.SSS A'
|
||||
}),
|
||||
align(),
|
||||
printf((info) => `${info.timestamp} - ${info.level}: ${info.message}`)
|
||||
),
|
||||
transports: [fileRotateTransport]
|
||||
});
|
||||
Reference in New Issue
Block a user