Always getting error "Cannot log unknown syslog level"
Opened this issue · 6 comments
Hi All,
I am always getting error "Cannot log unknown syslog level". When i look into the code, the following code snippet is reason for this error
if (!~levels.indexOf(info[LEVEL])) {
return callback(new Error('Cannot log unknown syslog level: ' + info[LEVEL]));
}
Can you please provide simple working sample?
Regards,
Mukilan
The following sample code i tried
`var winston = require('winston')
require('winston-syslog').Syslog;
winston.setLevels(winston.config.syslog);
var logger = new (winston.Logger)({
//levels: winston.config.syslog.levels,
transports: [
new (winston.transports.Syslog)({
host:'localhost',
port: 514,
level:'debug'
})
]
});
logger.debug('Hello distributed log files!')`
I am getting the same error.
same error with key warn
I have winston 2 and it started to work when i downgraded winston-syslog to version 1.2.6
The same thing
Having the same issue
`'use strict';
const os = require('os');
const winston = require('winston');
require('winston-syslog').Syslog;
/**
- Define error codes for easy use.
- @type {{NOCONFIG: string}} - PAPERTRAIL_HOST or PAPERTRAIL_PORT not defined in env variables.
*/
const errorCodes = {
NOCONFIG: 'PAPERTRAIL_HOST or PAPERTRAIL_PORT not defined in env variables.'
};
/**
- Validates the config for the syslog transport used to send logs to papertrail. Will either resolve if
- required environment variables are set, or will reject with an error is the variables are missing.
- Requires:
- process.env.PAPERTRAIL_HOST - host of syslog server
- process.env.PAPERTRAIL_PORT - port of syslog server
- @returns {Promise}
*/
const validateConfigForPapertrail = () => {
return new Promise((resolve, reject) => {
process.env.PAPERTRAIL_HOST && process.env.PAPERTRAIL_PORT ? resolve() : reject(errorCodes.NOCONFIG)
});
};
/**
- Adds the papertrail (syslog) transport to winston. Uses the host and port set in the envirnment variables. Can
- also use a hostname, and program name. Uses udp4 to connect to the syslog server. Will either resolve, or
- reject with an error of the transport fails to be created.
- Requires:
- process.env.PAPERTRAIL_HOST - host of syslog server
- process.env.PAPERTRAIL_PORT - port of syslog server
- Optional:
- process.env.PAPERTRAIL_HOSTNAME - name of system/machine
- process.env.PAPERTRAIL_PROGRAM - app name
- @returns {Promise}
*/
const addPapertrailTransport = () => {
return new Promise((resolve, reject) => {
try {
winston.add(new winston.transports.Syslog({
host: process.env.PAPERTRAIL_HOST,
port: process.env.PAPERTRAIL_PORT,
protocol: 'udp4',
pid: process.pid,
localhost: process.env.PAPERTRAIL_HOSTNAME || os.hostname(),
app_name: process.env.PAPERTRAIL_PROGRAM || 'default'
})
);
resolve();
} catch (error) {
reject(Error creating syslog transport for papertrail: ${error}
);
}
});
};
/**
- Adds a console transport to winston is process.env.NODE_ENV !== production. This allows you to
- see console logs in a non-prod environment.
- @returns {Promise}
*/
const addConsoleTransportIfDev = () => {
return new Promise((resolve, reject) => {
try {
if (process.env.NODE_ENV !== 'production') {
winston.add(new winston.transports.Console({
level: 'debug',
format: winston.format.combine(
winston.format.prettyPrint(),
winston.format.errors({stack: true})
),
}))
}
resolve();
} catch (error) {
reject(Error creating console transport for logging: ${error}
)
}
});
};
/**
- Composes and runs the functions on startup. First it adds the console
- transport if in a non prod environment. Then it validates the papertrail(syslog) config,
- and adds the syslog transport.
- @function init
*/
addConsoleTransportIfDev()
.then(validateConfigForPapertrail)
.then(addPapertrailTransport)
.catch(error => console.error(error));
/**
- Define the logger interface
- @type {{warn: logger.warn, debug: logger.debug, log: logger.log, error: logger.error, info: logger.info}}
/
const logger = {
/*- Logs a message, taking in a level and the message.
- @param level {string}
- @param message {string}
/
log: (level, message) => {winston.log(level, message)},
/* - Logs a debug message
- @param message {string}
/
debug: (message) => {winston.debug(message)},
/* - Logs an info message
- @param message {string}
/
info: (message) => {winston.info(message)},
warn: (message) => {winston.warn(message)},
/* - Logs an error, taking in a message and an error
- @param message {string}
- @param error {Error}
*/
error: (message, error) => {winston.error(message, error)}
};
/**
- Export the instance of logger
- @type {{}}
*/
module.exports = logger;
`