Skip to content

Instantly share code, notes, and snippets.

@eduardomarcos
Last active August 8, 2022 23:55
Show Gist options
  • Save eduardomarcos/702b0f2d0e2eefca5a72c2cdd28b818a to your computer and use it in GitHub Desktop.
Save eduardomarcos/702b0f2d0e2eefca5a72c2cdd28b818a to your computer and use it in GitHub Desktop.
Nodered Custom Logging Module for Loki (Grafana)
logging: {
// Only console logging is currently supported
console: {
level: "trace",
metrics: false,
audit: false
},
loki: {
level:'trace',
metrics:false,
audit:false,
handler: function(conf) {
var axios = require('axios');
return function(msg) {
switch (msg.level){
case 20:
msg.level = "error";
break;
case 30:
msg.level = "warn";
break;
case 40:
msg.level = "info";
break;
case 50:
msg.level = "debug";
break;
case 60:
msg.level = "trace";
break;
default:
break;
}
var row = JSON.stringify({
'prefix':msg.msg.prefix, //Optional for me. So anyone can use custom fields in here
'level':msg.level,
'name':msg.name,
'type':msg.type,
'msg':JSON.stringify(msg.msg)
});
var data = JSON.stringify({
'streams' : [
{
'stream': {
'job':'nodered'
},
'values':[[(msg.timestamp * 1000000).toString(), row]]
}
]
});
try {
//console.log(data);
axios.post('http://loki:3100/loki/api/v1/push', data, {
headers: {
'Content-Type': 'application/json',
}
}).then((res) => {
console.log(res.status);
})
.catch((error) => {
//console.error(error);
console.error(error.response.status + ", " + error.response.statusText);
})
} catch(err) { console.log(err); }
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment