Skip to content

Commit

Permalink
Add redis transport
Browse files Browse the repository at this point in the history
  • Loading branch information
adhisimon committed Sep 12, 2021
1 parent a2fc405 commit 532973f
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 1 deletion.
68 changes: 68 additions & 0 deletions examples/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const path = require('path');

// publish config object to be used by tektrans-logger
global.TEKTRANS_LOGGER_CONFIG = {
level: 'silly',
label: 'REDIS-EXAMPLE',

// default is "logs" directory on current workdir
directory: path.join(process.cwd(), 'logs'),

filename: 'redis-log',

// default is using generic level value
console_level: null,

// default is using generic level value
file_level: null,

// default is no old file removal
max_files: '10d',

redis: {
host: 'localhost',
port: 6379,
auth: null,
channel: null,
},
};

const logger = require('..');

const sleep = (ms) => new Promise((resolve) => {
setTimeout(() => {
resolve();
}, ms);
});

const main = async () => {
// await sleep(2000);

logger.silly('This is a silly log.');
logger.debug('This is a debug log.');
logger.verbose('This is a verbose log');

// looks like redis transport doesn't support log with metadata,
// log with metadata will be skipped by redis transport
logger.verbose('This is a verbose example of log with metadata', {
a: 'metadata1',
b: 'metadata2',
c: {
c1: 'compund',
},
});

logger.http('This in a http log');
logger.info('This is an info log');
logger.warn('This is an warn log');
logger.error('This in an error');

// give few seconds to make sure all logs
// has been emitted to redis before close the log
await sleep(2000);

// don't forget to close the logger at the end
logger.end();
};

main();
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const config = require('./lib/config');

const consoleTransport = require('./lib/transports/console');
const fileTransport = require('./lib/transports/daily-rotate-file');
const redisTransport = require('./lib/transports/redis');

const transports = ['Console'];

Expand All @@ -24,6 +25,14 @@ if (fileTransport) {
logger.debug(`${MODULE_NAME} A2646043: File transport added to logger`);
}

if (redisTransport) {
logger.add(redisTransport);
transports.push('Redis');
logger.debug(`${MODULE_NAME} 9795F5D2: Redis transport added to logger`, {
redisConfig: config.redis,
});
}

logger.verbose(`${MODULE_NAME} AB6F996E: Initialized`, {
config,
transports,
Expand Down
41 changes: 41 additions & 0 deletions lib/transports/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const WinstonRedis = require('winston-redis');

const config = require('../config');

const DEFAULT_REDIS_HOST = 'localhost';
const DEFAULT_REDIS_PORT = 6379;
const DEFAULT_REDIS_AUTH = null;
const DEFAULT_REDIS_CHANNEL = [
'TEKTRANS-LOGGER',
'B707E453',
(config.label || '').toUpperCase(),
].filter((item) => item).join('_');

if (!config.redis) {
module.exports = null;
} else {
if (typeof config.redis !== 'object') {
config.redis = {};
}

config.redis.level = config.redis.level || config.level;
config.redis.host = config.redis.host || DEFAULT_REDIS_HOST;
config.redis.port = config.redis.port || DEFAULT_REDIS_PORT;
config.redis.auth = config.redis.auth || DEFAULT_REDIS_AUTH;
config.redis.channel = config.redis.channel || DEFAULT_REDIS_CHANNEL;

const options = {
level: config.redis.level,
host: config.redis.host,
port: config.redis.port,
auth: config.redis.auth,
channel: config.redis.channel,
meta: {
pid: process.pid,
},
};

const transport = new WinstonRedis(options);

module.exports = transport;
}
25 changes: 25 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"dependencies": {
"mkdirp": "^1.0.4",
"winston": "^3.3.3",
"winston-daily-rotate-file": "^4.5.5"
"winston-daily-rotate-file": "^4.5.5",
"winston-redis": "^3.1.0"
}
}

0 comments on commit 532973f

Please sign in to comment.