Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #41

Merged
merged 8 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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