charles-zh / winston-mysql Goto Github PK
View Code? Open in Web Editor NEWMySQL transport plugin for winston logger
License: MIT License
MySQL transport plugin for winston logger
License: MIT License
Hi,
I made some changes to match my use which I hope others may find useful:
I needed a small addition...to add a column telling me which env the code is running in:
CREATE TABLE IF NOT EXISTS sys_logs_default (
id INT NOT NULL AUTO_INCREMENT,
env VARCHAR(100) default 0,
level VARCHAR(16) NOT NULL,
message VARCHAR(2048) NOT NULL,
meta VARCHAR(2048) NOT NULL,
timestamp DATETIME NOT NULL,
PRIMARY KEY (id));
so I tweaked the class to serve my needs
example use:
var ip = require("ip");
....
const myFormatter = format((info) => {
info.env = ip.address()
return info;
})();
...
const SqlLogger = createLogger({
...
format: combine(timestamp(), errors({ stack: true }), myFormatter, json()),
...
new winstonMysql({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: 'logging_nodejs',
table: 'sys_logs_default',
charset: 'utf8_general_ci',
fields: { env: 'env', level: 'level', meta: 'meta', message: 'message', timestamp: 'timestamp' }
})
...
in contructor - I allowed for semi-custom table structure thus allowing more columns to be used defined by user.
if (!options.fields) {
this.options.fields = {};
//use default names
this.fields = {
level: 'level',
meta: 'meta',
message: 'message',
timestamp: 'timestamp'
}
} else {
//use custom table field names
const { level, meta, message, timestamp } = this.options.fields;
this.fields = {
// for custome fields
...this.options.fields,
level,
meta,
message,
timestamp
}
}
const log = {};
log[this.fields.meta] = JSON.stringify(winstonMeta);
log[this.fields.level] = level;
log[this.fields.message] = message;
log[this.fields.timestamp] = new Date();
for (const key in info) {
if (!['meta', 'level', 'message', 'timestamp'].includes(key) && this.fields.hasOwnProperty(key)) {
log[key] = info[key]
}
}
I'd be happy to open a PR if the author finds these changes of interest.
I used the example code in the installation guide and everything works as expected but the script is not ending and keeps running.
When commenting out this part below nothing is really happening, but the script ends. So it has something to do with the actual logger function.
logger.debug(msg, {message: msg, type: 'demo'});
logger.error(msg, {message: msg, type: 'demo'});
logger.info(msg, {message: msg, type: 'demo'});
logger.warn(msg, {message: msg, type: 'demo'});
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.