Skip to content

Commit

Permalink
Merge pull request #26 from ciorg/better-logging
Browse files Browse the repository at this point in the history
Better logging
  • Loading branch information
ciorg authored Jul 18, 2020
2 parents ca88f99 + 96d8f88 commit cb7b964
Show file tree
Hide file tree
Showing 15 changed files with 945 additions and 115 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ node_modules/
# dont want to save images
static/rb_imgs/*
config.yaml
log.out
error
info
fatal
18 changes: 12 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
"name": "cactus_coin",
"version": "0.1.0",
"description": "my website and stuff",
"license": "UNLICENSED",
"main": "index.js",
"author": "ciorg",
"scripts": {
"build": "tsc --project tsconfig.json",
"dbAction": "node dist/scripts/db_actions",
"test": "jest tests"
"test": "jest tests",
"start-app": "pm2 start dist/index.js",
"restart-app": "pm2 restart dist/index.js",
"stop-app": "pm2 stop dist/index.js"
},
"jest": {
"testMatch": [
Expand All @@ -24,16 +28,18 @@
"express": "^4.17.1",
"express-session": "^1.17.1",
"express-validator": "^6.6.0",
"fs-extra": "^9.0.1",
"global": "^4.4.0",
"helmet": "^3.23.3",
"js-string-escape": "^1.0.1",
"js-yaml": "^3.14.0",
"mongoose": "^5.9.23",
"mongoose": "^5.9.24",
"multer": "^1.4.2",
"passport": "^0.4.1",
"passport-local-mongoose": "^6.0.1",
"rate-limiter-flexible": "^2.1.7",
"safe-regex": "^2.1.1"
"pm2": "^4.4.0",
"rate-limiter-flexible": "^2.1.9",
"safe-regex": "^2.1.1",
"serve-favicon": "^2.5.0"
},
"devDependencies": {
"@types/bunyan": "^1.8.6",
Expand All @@ -42,7 +48,6 @@
"@types/connect-mongodb-session": "^0.0.3",
"@types/express": "^4.17.7",
"@types/express-session": "^1.17.0",
"@types/fs-extra": "^9.0.1",
"@types/helmet": "^0.0.47",
"@types/js-string-escape": "^1.0.0",
"@types/js-yaml": "^3.12.5",
Expand All @@ -51,6 +56,7 @@
"@types/passport": "^1.0.4",
"@types/passport-local-mongoose": "^4.0.13",
"@types/safe-regex": "^1.1.2",
"@types/serve-favicon": "^2.5.0",
"jest": "^26.1.0",
"typescript": "^3.9.6"
}
Expand Down
12 changes: 5 additions & 7 deletions src/controllers/lib/actions.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import * as I from '../../interface';
import Logger from '../../utils/logger';
import Configs from '../../utils/configs';

class Actions {
log: Logger;
model: any;

constructor(model: any) {
const configs: Configs = new Configs();

this.log = new Logger(configs.getConfigs().log_path);
this.log = new Logger();

this.model = model;
}
Expand All @@ -20,11 +17,11 @@ class Actions {
try {
const res = await this.model[action](params);

this.log.info(`successfully completed ${action} for ${JSON.stringify(params)}`);
this.log.debug(`successfully completed ${action} for ${JSON.stringify(params)}`);

result.res = res;
} catch(e) {
this.log.error(e.message);
this.log.error(e.message, { err: e });
result.error = true;
}

Expand All @@ -41,8 +38,9 @@ class Actions {
if (Object.keys(params).length) {
try {
result.res = await this.model.updateOne({ _id: id }, params);
this.log.debug(`updated ${id}, ${params}`)
} catch (e) {
this.log.error(e.message);
this.log.error(e.message, { err: e });
result.error = true;
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/controllers/user.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Request } from 'express';
import userModel from '../models/user';
import Logger from '../utils/logger';
import Configs from '../utils/configs';
import * as I from '../interface';

class User {
log: Logger;

constructor() {
const configs = new Configs;
this.log = new Logger(configs.getConfigs().log_path);
this.log = new Logger();
}

async create(req: Request) {
Expand All @@ -31,11 +29,11 @@ class User {
created: new Date()
}, password);

this.log.info(`created: ${response.username}, id: ${response._id}`);
this.log.debug(`created: ${response.username}, id: ${response._id}`, { req });
result.res = response._id;
} catch (e) {
result.error = true;
this.log.error(e.message)
this.log.error('could not create user', { err: e, req });
}

return result;
Expand All @@ -60,6 +58,8 @@ class User {

await userM.save();

this.log.debug(`updated password`, { req });

result.res = 'password reset successfully';
} catch(e) {
if (e.message === 'Password or username is incorrect') {
Expand All @@ -69,7 +69,7 @@ class User {
}

result.error = true;
this.log.error(e.message)
this.log.error('could not update password', { err: e, req })
}

return result;
Expand Down
13 changes: 8 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import path from 'path';
import express, { Request, Response } from 'express';
import bodyParser from 'body-parser';
import favicon from 'serve-favicon';
import helmet from 'helmet';
import compression from 'compression';
import Configs from './utils/configs';
Expand All @@ -9,9 +11,9 @@ import authenticate from './utils/authenticate';
import routes from './routes/routes';

const configs = new Configs();
const { env, port, log_path } = configs.getConfigs();
const { env, port } = configs.getConfigs();

const logger = new Logger(log_path);
const logger = new Logger();

process.env.NODE_ENV = env;

Expand All @@ -24,13 +26,14 @@ app.use(express.static('static'));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(favicon(path.join(__dirname, '..', 'static', 'imgs', 'cc_logo.ico')));

app.use(authenticate);
app.use('/', routes);

app.use(function(req: Request, res: Response){
logger.error(`invalid path ${req.path}`)
app.use(function(req: Request, res: Response) {
logger.error(`invalid path ${req.path}`, { err: new Error('bad request'), req });
res.status(404).redirect('/error');
});

app.listen(port, () => logger.info(`App listening on port:${port}`));
app.listen(port, () => logger.info(`App listening on port:${port}`, {}));
15 changes: 14 additions & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Request, Response } from 'express';

export interface RootBeer {
_id: string;
name: string;
Expand Down Expand Up @@ -48,8 +50,19 @@ export interface WriteUp {
export interface ConfigSettings {
mongo_settings: { url: string, database: string};
env: string;
log_level: string;
log_level: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal' | number;
log_path: string;
secret: string;
port: number
}

export interface ErrorObject {
err: Error;
req?: Request;
res?: Response;
}

export interface LogObject {
req?: Request;
res?: Response;
}
13 changes: 13 additions & 0 deletions src/models/lib/db_api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
const mongoose = require('mongoose');
import Configs from '../../utils/configs';
import Logger from '../../utils/logger';

// sudo docker exec -it 768a2742624a mongo

class MongooseApi {
log: Logger;

constructor() {
const configs = new Configs();
const { mongo_settings } = configs.getConfigs();

this.log = new Logger();

mongoose.connect(
`mongodb://${mongo_settings.url}/${mongo_settings.database}`,
Expand All @@ -16,6 +21,14 @@ class MongooseApi {
useUnifiedTopology: true
}
);

const db = mongoose.connection;

db.on('error', (error: Error) => {
this.log.fatal('could not connect to db', { err: error });
});

db.once('open', () => { this.log.debug(`connected to ${mongo_settings.url}/${mongo_settings.database}`)});
}

schema(schema: any) {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ router.get('/',

router.post('/portal', login);

router.get('/logout', (req, res) => {
router.get('/logout', (req: Request, res: Response) => {
req.logout();
res.redirect('/');
});
Expand Down
4 changes: 4 additions & 0 deletions src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import rb from './rb';
import rate from './ratings';
import pub from './public';
import writeUp from './write_up';
import Logger from '../utils/logger';

const log = new Logger();

const router = express.Router();

Expand All @@ -17,6 +20,7 @@ router.use(writeUp);
router.use(pub);

router.get('/error', (req: Request, res: Response) => {
log.error('bad request', { err: new Error('bad request'), req })
res.render('pages/error', { message: undefined });
});

Expand Down
7 changes: 3 additions & 4 deletions src/utils/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import Logger from './logger';

const config = new Configs();

const { secret, log_path, mongo_settings } = config.getConfigs();
const { secret, mongo_settings } = config.getConfigs();

const logger = new Logger(log_path);
const logger = new Logger();

const router = express.Router();

Expand Down Expand Up @@ -41,8 +41,7 @@ passport.serializeUser(userModel.serializeUser());
passport.deserializeUser(userModel.deserializeUser());

function mongoError(err: Error) {
logger.error(err.message);
process.exit(1);
logger.fatal('could not connect to db', { err });
}

export = router;
2 changes: 1 addition & 1 deletion src/utils/configs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import jsYaml from 'js-yaml';
import path from 'path';
import fs from 'fs-extra';
import fs from 'fs';
import * as I from '../interface';

class Configs {
Expand Down
49 changes: 40 additions & 9 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,60 @@
import path from 'path';
import bunyan from 'bunyan';
import Configs from './configs';
import * as I from '../interface';

class Logger {
logger: bunyan
constructor(path: string) {
configs: Configs;

constructor() {
this.configs = new Configs();

const { log_path, log_level } = this.configs.getConfigs();

this.logger = bunyan.createLogger({
name: 'rb_lvl',
name: 'rb_site',
streams: [
{
level: 'info',
stream: process.stdout
level: log_level,
path: path.join(log_path, 'info'),
type: 'rotating-file',
period: '1m',
count: 12
},
{
level: 'error',
path
path: path.join(log_path, 'error'),
type: 'rotating-file',
period: '1m',
count: 12
},
{
level: 'fatal',
path: path.join(log_path, 'fatal'),
type: 'rotating-file',
period: '1m',
count: 12
}
]
});
}

info(msg: string) {
this.logger.info(msg);
debug(msg: string, logObj: I.LogObject = { res: undefined, req: undefined }) {
this.logger.debug(logObj, msg);
}

info(msg: string, logObj: I.LogObject = { res: undefined, req: undefined }) {
this.logger.info(logObj, msg);
}

error(msg: string, errObj: I.ErrorObject) {
this.logger.error(errObj, msg);
}

error(msg: string) {
this.logger.error(msg);
fatal(msg:string, fatalObj: I.ErrorObject) {
this.logger.fatal(fatalObj, msg);
process.exit(1);
}
}

Expand Down
Loading

0 comments on commit cb7b964

Please sign in to comment.