Skip to content

Commit

Permalink
[#7] refactor: move logger into seperate common folder
Browse files Browse the repository at this point in the history
  • Loading branch information
glemenneo committed Sep 18, 2024
1 parent 831983b commit 7942e03
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
5 changes: 5 additions & 0 deletions backend/user-service/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MongoDBContainer, StartedMongoDBContainer } from '@testcontainers/mongodb'
import request from 'supertest'
import logger from '../src/common/logger.util'
import app from '../src/index'

describe('Index', () => {
Expand All @@ -8,11 +9,15 @@ describe('Index', () => {
beforeAll(async () => {
const container: MongoDBContainer = new MongoDBContainer().withExposedPorts(27017)
startedContainer = await container.start()
logger.info(
`[Index Test] MongoDB container started on ${startedContainer.getHost()}:${startedContainer.getMappedPort(27017)}`
)
process.env.DB_URL = `mongodb://${startedContainer.getHost()}:${startedContainer.getMappedPort(27017)}/user-service`
})

afterAll(async () => {
await startedContainer.stop()
logger.info(`[Index Test] MongoDB container stopped`)
})

describe('GET /', () => {
Expand Down
12 changes: 12 additions & 0 deletions backend/user-service/src/common/logger.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Winston from 'winston'

export default Winston.createLogger({
level: 'info',
format: Winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
new Winston.transports.Console(),
new Winston.transports.File({ filename: 'error.log', level: 'error' }),
new Winston.transports.File({ filename: 'combined.log' }),
],
})
2 changes: 1 addition & 1 deletion backend/user-service/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cors from 'cors'
import express, { Express, NextFunction, Request, Response } from 'express'
import helmet from 'helmet'
import { logger } from './server'
import logger from './common/logger.util'

const app: Express = express()

Expand Down
14 changes: 1 addition & 13 deletions backend/user-service/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import 'dotenv/config'
import http, { Server } from 'http'
import { connect } from 'mongoose'
import Winston, { Logger } from 'winston'
import logger from './common/logger.util'
import index from './index'

// Setup Logger
export const logger: Logger = Winston.createLogger({
level: 'info',
format: Winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
new Winston.transports.Console(),
new Winston.transports.File({ filename: 'error.log', level: 'error' }),
new Winston.transports.File({ filename: 'combined.log' }),
],
})

const port: string = process.env.PORT ?? '3000'
const dbUrl: string | undefined = process.env.DB_URL
if (!dbUrl) {
Expand Down

0 comments on commit 7942e03

Please sign in to comment.