Skip to content

Commit

Permalink
Now logger handles and logs unexpected errors too
Browse files Browse the repository at this point in the history
  • Loading branch information
javieraviles committed Apr 7, 2020
1 parent d78dd8d commit c93880d
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Context } from "koa";
import { config } from "./config";
import { transports, format } from "winston";
import * as path from "path";

const logger = (winstonInstance: any): any => {
winstonInstance.configure({
level: config.debugLogging ? "debug" : "info",
transports: [
//
// - Write all logs error (and below) to `error.log`.
new transports.File({ filename: "error.log", level: "error" }),
new transports.File({ filename: path.resolve(__dirname, "../error.log"), level: "error" }),
//
// - Write to all logs with specified level to console.
new transports.Console({
Expand All @@ -21,11 +22,14 @@ const logger = (winstonInstance: any): any => {
});

return async (ctx: Context, next: () => Promise<any>): Promise<void> => {

const start = new Date().getTime();

await next();

try {
await next();
} catch (err) {
ctx.status = err.status || 500;
ctx.body = err.message;
}
const ms = new Date().getTime() - start;

let logLevel: string;
Expand Down

0 comments on commit c93880d

Please sign in to comment.