Skip to content

Commit

Permalink
move winston logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua2504 committed Aug 27, 2024
1 parent d9381ce commit 8b866bb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
5 changes: 3 additions & 2 deletions server/dbInit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const mysql = require('mysql');
const logger = require('./winstonLogger');

require('dotenv').config();
const DB_HOST = process.env.DB_HOST;
Expand All @@ -17,10 +18,10 @@ const connection = mysql.createConnection({

connection.connect((err) => {
if (err) {
console.error('Error connecting to the database: ' + err.stack);
logger.error('Error connecting to database: ' + err);
return;
}
console.log('Connected to the database as id ' + connection.threadId);
logger.info('Connected to database ' + DB_NAME + ' on ' + DB_HOST + ':' + DB_PORT + ' as user ' + DB_USER);
});

module.exports = connection;
21 changes: 1 addition & 20 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
// this nodejs server will serve a rest api for the frontend to consume
// everything will be in one file for simplicity

// Winston logger
const winston = require('winston');

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

if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.simple(),
}));
}
const logger = require('./winstonLogger');

// Environment variables
require('dotenv').config();
Expand Down
24 changes: 24 additions & 0 deletions server/winstonLogger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Winston logger
const winston = require('winston');

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


if (process.env.NODE_ENV !== 'production') {
console.log('Not in production');
logger.add(new winston.transports.Console({
format: winston.format.simple(),
}));
}

module.exports = logger;

0 comments on commit 8b866bb

Please sign in to comment.