Skip to content

Commit

Permalink
feat: add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehmet Sayin authored and Mehmet Sayin committed Apr 6, 2024
1 parent dbfee51 commit 616d4fc
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 32 deletions.
11 changes: 11 additions & 0 deletions backend/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request, Response } from 'express';
import 'express-async-errors';
const cookieParser = require('cookie-parser');
import morgan from 'morgan';

import routes from './routes';
import { json } from 'body-parser';
Expand All @@ -11,13 +12,17 @@ import path from 'path';
import { NotFoundError } from './errors/not-found-error';
import { errorHandler } from './middleware/error-handler';
import { updateMetrics } from './metrics';
import { logger } from './logger';
import winston from 'winston';

const app = express();

app.set('trust proxy', true);
app.use(json());
app.use(cookieParser());
app.use(express.json());
app.use(morgan('dev'));


app.use(
cors({
Expand All @@ -44,4 +49,10 @@ if (process.env.NODE_ENV === 'production') {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}

if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.simple(),
}));
}
export { app };
9 changes: 9 additions & 0 deletions backend/combined.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{"level":"error","message":"Invalid credentials"}
{"level":"error","statusCode":400}
{"level":"error","message":"User not found"}
{"level":"error","statusCode":400}
{"level":"error","message":"User not found"}
{"level":"error","message":"User not found"}
{"level":"error","message":"User not found"}
{"level":"error","statusCode":400}
{"level":"error","message":"Login attempt failed for username: fdsd"}
33 changes: 18 additions & 15 deletions backend/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,30 @@ import {
import { CustomError } from '../errors/custom-error';
import { generateAccessToken, generateRefreshToken } from '../utils/jwt.utils';
import { refreshToken } from '../services/user/auth-v2.service';
import { logger } from '../logger';

export const loginController = async (req: Request, res: Response) => {
const { username, password } = req.body;
const result = await loginUser(username, password);
const accessToken = generateAccessToken(result.user);

const refreshToken = generateRefreshToken(result.user);
const { username, password } = req.body;
const result = await loginUser(username, password);
const accessToken = generateAccessToken(result.user);

res.cookie('refreshToken', refreshToken, {
httpOnly: true,
secure: true,
sameSite: 'none',
});
const refreshToken = generateRefreshToken(result.user);

res.cookie('accessToken', accessToken, {
httpOnly: true,
secure: true,
sameSite: 'none',
});
res.cookie('refreshToken', refreshToken, {
httpOnly: true,
secure: true,
sameSite: 'none',
});

res.cookie('accessToken', accessToken, {
httpOnly: true,
secure: true,
sameSite: 'none',
});

res.status(201).json(result);

res.status(201).json(result);
};

export const registerController = async (req: Request, res: Response) => {
Expand Down
9 changes: 9 additions & 0 deletions backend/error.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{"level":"error","message":"Invalid credentials"}
{"level":"error","statusCode":400}
{"level":"error","message":"User not found"}
{"level":"error","statusCode":400}
{"level":"error","message":"User not found"}
{"level":"error","message":"User not found"}
{"level":"error","message":"User not found"}
{"level":"error","statusCode":400}
{"level":"error","message":"Login attempt failed for username: fdsd"}
1 change: 1 addition & 0 deletions backend/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import mongoose from 'mongoose';


import { app } from './app';
import { DatabaseConnectionError } from './errors/database-connection-error';
import { myCronJob } from './cronJob';
Expand Down
10 changes: 10 additions & 0 deletions backend/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import winston from "winston";

export const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' }),
],
});
Loading

0 comments on commit 616d4fc

Please sign in to comment.