Skip to content

Commit

Permalink
Merge pull request #147 from halkeye/log-to-loki
Browse files Browse the repository at this point in the history
Add support for loki logger
  • Loading branch information
niekcandaele committed Jul 10, 2020
2 parents 2bbb44e + e8c4f41 commit 98318cf
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
78 changes: 78 additions & 0 deletions api/winston-loki-transport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const winston = require('winston');
const request = require('request');

const loadNs = process.hrtime();
const loadMs = new Date().getTime();

function nanoseconds() {
const diffNs = process.hrtime(loadNs);
const part2 = BigInt(diffNs[0]) * BigInt(1e9) + BigInt(diffNs[1]);
return (BigInt(loadMs) * BigInt(1e6) + part2).toString();
}

function quote(str) {
if (!str) {
return str;
}
return `"${str.replace(/"/g, '"')}"`;
}

function errorToString(err) {
if (err.stack) {
return `message=${quote(err.message)},code=${quote(err.code)},stack=${quote(err.stack)}`;
}
return err.toString();
}

class LokiTransport extends winston.transports.Http {
constructor(options) {
super(options);
this.lokiURL = options.lokiURL;
this.labels = options.labels;
this.labels.instance = this.labels.instance.replace(/https?:\/\//g, '').replace(/\//g, '');
}

_request(options, callback) {

// handle errors
if (options.params.meta instanceof Error) {
options.params.message = options.params.message + errorToString(options.params.meta);
options.params.meta = {
};
}
options.params.meta.level = options.params.level;

const config = {
url: this.lokiURL,
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'streams': [
{
'stream': {
...this.labels,
level: options.params.level,
},
'values': [
[
nanoseconds(),
`${options.params.level}: ${options.params.message}`
],
]
}
]
})
};
request(config, function (err, res, body) {
if (err) {
console.error({ err, body, config });
}
callback(err, res, body);
});
}
}


module.exports = LokiTransport;
17 changes: 17 additions & 0 deletions config/customLog.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const winston = require('winston');
const LokiTransport = require('../api/winston-loki-transport');

const logLevel = process.env.CSMM_LOGLEVEL || 'info';

const infoAndAbove = ['info', 'warn', 'blank', 'crit'];

const shouldLogJSON = JSON.parse((process.env.CSMM_LOG_JSON || 'false').toLowerCase());

const transports = [
new winston.transports.File({
level: 'info',
Expand All @@ -19,12 +22,26 @@ const transports = [
}),
new winston.transports.Console({
level: logLevel,
json: shouldLogJSON,
stringify: !shouldLogJSON ? undefined: (obj) => JSON.stringify(obj),
colorize: true,
timestamp: true,
humanReadableUnhandledException: true
})
];

if (process.env.LOKI_URL) {
transports.push(new LokiTransport({
level: logLevel,
labels: {
app: 'csmm',
job: 'csmm',
instance: process.env.CSMM_HOSTNAME || os.hostname(),
},
lokiURL: process.env.LOKI_URL,
}));
}

if (!infoAndAbove.includes(logLevel)) {
transports.push(
new winston.transports.File({
Expand Down

0 comments on commit 98318cf

Please sign in to comment.