Skip to content
Open
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
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ GRAYLOG_HOSTNAME=sayan
GRAYLOG_FACILITY=worker
ENV=development
# Accepted log levels: alert, critical, error, warning, notice, info, debug
LOG_LEVEL=
LOG_LEVEL=debug
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
package-lock.json
test.js
test.js
.env
69 changes: 38 additions & 31 deletions log.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
const LOG_LEVEL = process.env.LOG_LEVEL;
var grayLogger;
const graylog2 = require("graylog2");
/**
* graylog.emergency
* graylog.alert
* graylog.critical
* graylog.error
* graylog.warning
* graylog.notice
* graylog.info
* graylog.debug
*/
graylog.alert
graylog.critical
graylog.error
graylog.warning
graylog.notice
graylog.info
graylog.debug
* */
const graylogEnums = {
alert: 0,
critical: 0,
notice: 0,
emergency: 0,
error: 0,
warning: 1,
info: 2,
debug: 3
};

const graylog2 = require("graylog2");
const grayLogger = new graylog2.graylog({
servers: [
{ 'host': process.env.GRAYLOG_HOST || 'localhost', port: process.env.GRAYLOG_PORT || '12201' },
],
hostname: process.env.GRAYLOG_HOSTNAME || 'tinker20',
facility: process.env.GRAYLOG_FACILITY || 'worker',
bufferSize: 1350
});
grayLogger = grayLogger || new graylog2.graylog({
servers: [
{'host': process.env.GRAYLOG_HOST || 'localhost', port: process.env.GRAYLOG_PORT || '12201'},
],
hostname: process.env.GRAYLOG_HOSTNAME || 'tinker20',
facility: process.env.GRAYLOG_FACILITY || 'worker',
bufferSize: 1350
});

/**
* error
* warn
* info
* verbose
* debug
* silly - Not supported currently.
*/

const winston = require("winston");

const winstonEnums = {
close: 'close',
emergency: 'error',
alert: 'error',
critical: 'error',
Expand All @@ -43,13 +48,15 @@ const winstonEnums = {

const winstonLogger = winston.createLogger({
transports: [
new winston.transports.Console({level: winstonEnums[process.env.LOG_LEVEL]})
new winston.transports.Console({level: winstonEnums[process.env.LOG_LEVEL]})
]
});
});

const logger = function(level, message){
if(process.env.ENV === 'production'){
grayLogger[level](message);
const logger = function (level, message) {
if (process.env.ENV === 'production') {
if (graylogEnums[level] <= graylogEnums[LOG_LEVEL.toLowerCase()]) {
grayLogger[level](message);
}
}
else {
winstonLogger[winstonEnums[level]](message);
Expand All @@ -60,4 +67,4 @@ grayLogger.on('error', function (error) {
console.error('Error while trying to write to graylog2:', error);
});

module.exports = logger;
module.exports = logger;
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
"winston",
"logging"
],
"repository": {
"type": "git",
"url": "git+https://github.com/tinker20/log-switcher.git"
},
"author": "Sayan Bhattacharya <bhattacharyasayan.21@gmail.com>",
"license": "ISC",
"dependencies": {
"graylog2": "git@github.com:Wizcorp/node-graylog2.git#817cd0d",
"graylog2": "https://github.com/Wizcorp/node-graylog2#817cd0d",
"winston": "3.2.1"
}
}