Skip to content
This repository has been archived by the owner on May 6, 2019. It is now read-only.

Commit

Permalink
added file logger
Browse files Browse the repository at this point in the history
  • Loading branch information
natnat-mc committed Jan 12, 2019
1 parent 0114e8f commit c0ff909
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
3 changes: 3 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

"db": {
"filename": "data/db.sqlite"
},
"log": {
"filename": "kfet.log"
}
}

6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const log=require('log').get('index');

// load config
const config=require('./config');
log.info("Loaded configuration file");

// load evironment variables
let env=Object.assign({}, config.env, process.env);
Expand All @@ -16,14 +15,17 @@ process.chdir(__dirname);
// load shared file
const shared=require('./shared');
shared.config=function(namespace, key) {
return config[namespace][key];
return (config[namespace] || {})[key];
};

// create global event emitter
const EE=require('events');
const events=new EE();
shared.events=events;

// load logger
require('./src/logger')(shared.config('log', 'filename'), shared.config('log', 'mode'));

// load database
require('./src/db');

Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js 2>&1 | tee -a kfet.log",
"daemon": "nohup node index.js 2>&1 >> kfet.log&"
"start": "node index.js",
"daemon": "nohup node index.js 2>&1 >/dev/null &"
},
"repository": {
"type": "git",
Expand All @@ -28,6 +28,7 @@
"express-ws": "^4.0.0",
"log": "^3.2.0",
"log-node": "^3.2.1",
"printf": "^0.5.1",
"semver": "^5.6.0"
},
"devDependencies": {
Expand Down
37 changes: 37 additions & 0 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const log=require('log');
const emitter=require('log/writer-utils/emitter');
const levels=require('log/levels');
const printf=require('printf');
const fs=require('fs');

let fd;
module.exports=function(filename, mode) {
if(fd) throw new Error("Already open");

fd=fs.openSync(filename, 'a', mode || 0o755);

let minLvl=process.env.LOG_LEVEL || 'notice';
if(typeof minLvl=='string') minLvl=levels.indexOf(minLvl);
if(minLvl<0) minLvl=0;

emitter.on('log', msg => {
if(msg.logger.levelIndex>=minLvl) {
let now=new Date();
let txt=printf.apply(null, [
"%02d/%02d/%d %02d:%02d:%02d %s %s %s\n",
now.getDate(),
now.getMonth()+1,
now.getFullYear(),
now.getHours(),
now.getMinutes(),
now.getSeconds(),
msg.logger.level.toUpperCase(),
msg.logger.namespace,
printf.apply(null, msg.messageTokens)
]);
fs.writeSync(fd, txt);
}
});

process.on('exit', () => fs.closeSync(fd));
};

0 comments on commit c0ff909

Please sign in to comment.