Skip to content

Commit

Permalink
Merge pull request javieraviles#41 from javieraviles/develop
Browse files Browse the repository at this point in the history
Release 1.4.2
  • Loading branch information
javieraviles authored Jul 16, 2019
2 parents f6dffb0 + 34d3eb0 commit 8d75cff
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ To install or update these dependencies you can use `npm install` or `npm update

## Changelog

### 1.4.2
- Fix -> `npm run watch-server` is now working properly live-reloading changes in the code [Issue 39](https://github.com/javieraviles/node-typescript-koa-rest/issues/39).
- Fix -> Logging levels were not correctly mapped. Thanks to @atamano for the PR [Pull Request 35](https://github.com/javieraviles/node-typescript-koa-rest/pull/35)
- Some code leftovers removed

### 1.4.1
- Fix -> After updating winston to 3.0.0, it was throwing an error when logging errors into file
- Fix -> Config in config.ts wasn't implementing IConfig interface
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "node-typescript-koa-rest",
"version": "1.4.1",
"version": "1.4.2",
"description": "API REST using NodeJS and KOA framework, typescript. TypeORM for SQL with class-validators. Middlewares JWT, CORS, Winston Logger.",
"main": "dist/server.js",
"scripts": {
"watch-server": "nodemon --watch 'src/**/*' -e ts,tsx --exec ts-node src/server.ts",
"watch-server": "nodemon --watch src -e ts,tsx --exec ts-node src/server.ts",
"build-ts": "tsc",
"copy-static-assets": "ts-node copyStaticAssets.ts",
"tslint": "tslint -c tslint.json -p tsconfig.json",
Expand Down
38 changes: 18 additions & 20 deletions src/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ import { config } from './config';
import * as winston from 'winston';

export function logger(winstonInstance) {
winstonInstance.configure({
level: config.debugLogging ? 'debug' : 'info',
transports: [
//
// - Write all logs error (and below) to `error.log`.
new winston.transports.File({ filename: 'error.log', level: 'error' }),
//
// - Write to all logs with specified level to console.
new winston.transports.Console({ format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
) })
]
});

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

const start = new Date().getMilliseconds();
Expand All @@ -14,31 +29,14 @@ export function logger(winstonInstance) {
let logLevel: string;
if (ctx.status >= 500) {
logLevel = 'error';
}
if (ctx.status >= 400) {
} else if (ctx.status >= 400) {
logLevel = 'warn';
}
if (ctx.status >= 100) {
} else if (ctx.status >= 100) {
logLevel = 'info';
}

const msg: string = `${ctx.method} ${ctx.originalUrl} ${ctx.status} ${ms}ms`;

winstonInstance.configure({
level: config.debugLogging ? 'debug' : 'info',
transports: [
//
// - Write all logs error (and below) to `error.log`.
new winston.transports.File({ filename: 'error.log', level: 'error' }),
//
// - Write to all logs with specified level to console.
new winston.transports.Console({ format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
) })
]
});

winstonInstance.log(logLevel, msg);
};
}
}
4 changes: 0 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as bodyParser from 'koa-bodyparser';
import * as helmet from 'koa-helmet';
import * as cors from '@koa/cors';
import * as winston from 'winston';
import * as dotenv from 'dotenv';
import { createConnection } from 'typeorm';
import 'reflect-metadata';
import * as PostgressConnectionStringParser from 'pg-connection-string';
Expand All @@ -13,9 +12,6 @@ import { logger } from './logging';
import { config } from './config';
import { router } from './routes';

// Load environment variables from .env file, where API keys and passwords are configured
dotenv.config({ path: '.env' });

// Get DB connection options from env variable
const connectionOptions = PostgressConnectionStringParser.parse(config.databaseUrl);

Expand Down

0 comments on commit 8d75cff

Please sign in to comment.