API-14: Changing logging to winston

This commit is contained in:
Patrick Müller 2021-08-20 14:09:57 +02:00
parent 7265f92486
commit 80006f7cb4
12 changed files with 2043 additions and 26 deletions

5
app.ts
View File

@ -5,11 +5,12 @@ import * as dotenv from 'dotenv';
import {partyPlanerRouter} from './src/models/partyplaner/PartyPlaner.router';
import {highlightMarkerRouter} from './src/models/twitch-highlight-marker/HighlightMarker.router';
import {dhbwServiceRouter} from './src/models/dhbw-service/DHBWService.router';
import logger from "./src/middleware/logger";
dotenv.config();
if (!process.env.PORT) {
console.log('No port');
logger.error('No port');
process.exit(1);
}
@ -32,5 +33,5 @@ app.get('/', (req: express.Request, res: express.Response) => {
});
server.listen(port, () => {
console.log('Server listening on Port ' + port);
logger.info('Server listening on Port ' + port);
});

1977
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -13,17 +13,21 @@
"author": "",
"license": "ISC",
"dependencies": {
"app-root-path": "^3.0.0",
"bcrypt": "^5.0.1",
"debug": "^4.3.1",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"guid-typescript": "^1.0.9",
"mariadb": "^2.5.3"
"mariadb": "^2.5.3",
"winston": "^3.3.3"
},
"devDependencies": {
"@types/app-root-path": "^1.2.4",
"@types/bcrypt": "^3.0.1",
"@types/debug": "^4.1.5",
"@types/express": "^4.17.11",
"@types/winston": "^2.4.4",
"source-map-support": "^0.5.19",
"tslint": "^6.1.3",
"typescript": "^4.1.5"

52
src/middleware/logger.ts Normal file
View File

@ -0,0 +1,52 @@
import * as appRoot from "app-root-path";
import * as winston from "winston";
const options = {
file_info: {
level: 'info',
filename: `${appRoot}/logs/app.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false
},
file_error: {
level: 'error',
filename: `${appRoot}/logs/error.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false
},
file_debug: {
level: 'debug',
filename: `${appRoot}/logs/debug.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false
},
console: {
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
},
};
const logger: winston.Logger = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
),
transports: [
new winston.transports.File(options.file_info),
new winston.transports.File(options.file_error),
new winston.transports.File(options.file_debug),
new winston.transports.Console(options.console)
],
exitOnError: false, // do not exit on handled exceptions
});
export default logger;

View File

@ -3,6 +3,7 @@
*/
import express, {Request, Response} from 'express';
import {generalInfoRouter} from './generalInfo/GeneralInfo.router';
import logger from "../../middleware/logger";
/**
* Router Definition
@ -16,7 +17,7 @@ dhbwServiceRouter.get('/', async (req: Request, res: Response) => {
try {
res.status(200).send('Pluto Development DHBW Service App API Endpoint');
} catch (e) {
console.log('Error handling a request: ' + e.message);
logger.error('Error handling a request: ' + e.message);
res.status(500).send({'message': 'Internal Server Error. Try again later.'});
}
})

View File

@ -2,6 +2,7 @@
* Required External Modules and Interfaces
*/
import express, {Request, Response} from 'express';
import logger from "../../../middleware/logger";
/**
* Router Definition
@ -12,7 +13,7 @@ generalInfoRouter.get('/', async (req: Request, res: Response) => {
try {
res.status(200).send('GET generalInfo v2.1');
} catch (e) {
console.log('Error handling a request: ' + e.message);
logger.error('Error handling a request: ' + e.message);
res.status(500).send({'message': 'Internal Server Error. Try again later.'});
}
})
@ -21,7 +22,7 @@ generalInfoRouter.post('/', async (req: Request, res: Response) => {
try {
res.status(200).send('POST generalInfo v2.1');
} catch (e) {
console.log('Error handling a request: ' + e.message);
logger.error('Error handling a request: ' + e.message);
res.status(500).send({'message': 'Internal Server Error. Try again later.'});
}
})

View File

@ -4,6 +4,7 @@
import express, {Request, Response} from 'express';
import {dataRouter} from './data/Data.router';
import {registerRouter} from './register/Register.router';
import logger from "../../middleware/logger";
/**
* Router Definition
@ -18,7 +19,7 @@ partyPlanerRouter.get('/', async (req: Request, res: Response) => {
try {
res.status(200).send('Pluto Development PartyPlaner API Endpoint V2');
} catch (e) {
console.log('Error handling a request: ' + e.message);
logger.error('Error handling a request: ' + e.message);
res.status(500).send({'message': 'Internal Server Error. Try again later.'});
}
})

View File

@ -2,6 +2,7 @@
* Required External Modules and Interfaces
*/
import express, {Request, Response} from 'express';
import logger from "../../../middleware/logger";
/**
* Router Definition
@ -12,7 +13,7 @@ dataRouter.get('/', async (req: Request, res: Response) => {
try {
res.status(200).send('GET data');
} catch (e) {
console.log('Error handling a request: ' + e.message);
logger.error('Error handling a request: ' + e.message);
res.status(500).send({'message': 'Internal Server Error. Try again later.'});
}
})
@ -21,7 +22,7 @@ dataRouter.post('/', async (req: Request, res: Response) => {
try {
res.status(200).send('POST data');
} catch (e) {
console.log('Error handling a request: ' + e.message);
logger.error('Error handling a request: ' + e.message);
res.status(500).send({'message': 'Internal Server Error. Try again later.'});
}
})

View File

@ -3,6 +3,7 @@
*/
import express, {Request, Response} from 'express';
import * as UserService from '../userService/user.service';
import logger from "../../../middleware/logger";
/**
* Router Definition
@ -61,7 +62,7 @@ registerRouter.post('/:isDevCall', async (req: Request, res: Response) => {
res.status(201).send(session);
} catch (e) {
console.log('Error handling a request: ' + e.message);
logger.error('Error handling a request: ' + e.message);
res.status(500).send({'message': 'Internal Server Error. Try again later.'});
}
})

View File

@ -55,7 +55,7 @@ export const getExistingUsernamesAndEmails = async (useDev: boolean): Promise<an
}
/**
* Used in the checkUsernameAndEmail method as return value
* Used in the registerUser and loginUser methods as return value
*/
export interface Session {
userId: string;

View File

@ -3,6 +3,7 @@
*/
import express, {Request, Response} from 'express';
import {addHighlightRouter} from './addHighlight/AddHighlight.router';
import logger from "../../middleware/logger";
/**
* Router Definition
@ -15,7 +16,7 @@ highlightMarkerRouter.get('/', async (req: Request, res: Response) => {
try {
res.status(200).send('Pluto Development Twitch Highlight Marker API Endpoint');
} catch (e) {
console.log('Error handling a request: ' + e.message);
logger.error('Error handling a request: ' + e.message);
res.status(500).send({'message': 'Internal Server Error. Try again later.'});
}
})

View File

@ -3,6 +3,7 @@
*/
import express, {Request, Response} from 'express';
import * as AddHighlightService from './addHighlights.service';
import logger from "../../../middleware/logger";
/**
* Router Definition
@ -13,7 +14,7 @@ addHighlightRouter.get('/', (req: Request, res: Response) => {
try {
res.status(200).send('GET endpoint not defined.');
} catch (e) {
console.log('Error handling a request: ' + e.message);
logger.error('Error handling a request: ' + e.message);
res.status(500).send({'message': 'Internal Server Error. Try again later.'});
}
})
@ -39,7 +40,7 @@ addHighlightRouter.post('/', (req: Request, res: Response) => {
res.status(200).send({'status': 'success', 'description': ''});
}
} catch (e) {
console.log('Error handling a request: ' + e.message);
logger.error('Error handling a request: ' + e.message);
res.status(500).send({'message': 'Internal Server Error. Try again later.'});
}
})