Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always getting error "Cannot log unknown syslog level" #97

Open
MukilanP opened this issue Mar 1, 2018 · 6 comments
Open

Always getting error "Cannot log unknown syslog level" #97

MukilanP opened this issue Mar 1, 2018 · 6 comments

Comments

@MukilanP
Copy link

MukilanP commented Mar 1, 2018

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

@MukilanP
Copy link
Author

MukilanP commented Mar 1, 2018

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!')`

@vlastoun
Copy link

vlastoun commented May 7, 2018

I am getting the same error.

@TuxGit
Copy link

TuxGit commented May 11, 2018

same error with key warn

@vlastoun
Copy link

I have winston 2 and it started to work when i downgraded winston-syslog to version 1.2.6

@gigi
Copy link

gigi commented Jul 3, 2018

The same thing

@Jesspu
Copy link

Jesspu commented Mar 27, 2019

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;
    `

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants