How to use winston in NodeJs?









up vote
0
down vote

favorite












I am trying to create a sample nodeJs program to use and understand winston library for nodeJs,

I had a sample code from one of the existing projects and I am trying to create a sample code sample,

The code contains winston configuration and a simple test function to print logs using different log levels.

I'm using node version 4.6.2, I know it's pretty old but, the existing application is using the same, therefore I wanted to understand the implementation on the same version,
The example consists of the following code and a log folder, in which I wish to print the log.



Following is a sample code which I am trying to run -



var winston = require('winston');
var envData = require('dotenv').config(path: 'server.env')
require('winston-daily-rotate-file');

var levelLog = 'debug';
var winstonTransports = ;

const logDTFormat = () => (new Date().toFormat('DD MMM YYYY HH24:MI:SS'));

// Winston Rotate File Logs
var transportDailyRotate = new (winston.transports.DailyRotateFile)(
filename: './logs/hrfid_app_',
datePattern: 'yyyy-MM-dd.log',
prepend: false,
level: levelLog,
timestamp: function()
var dateTime = new Date(Date.now());
return dateTime.toFormat('DD/MM/YYYY HH24:MI:SS');

);

winstonTransports.push(transportDailyRotate);

// Winston Console Log
var trasportConsole = new (winston.transports.Console)(
timestamp: logDTFormat,
colorize: true,
level: levelLog
);

winstonTransports.push(trasportConsole);

// Winston Config
winston.configure(
level: levelLog,
transports: winstonTransports
);

const logger = winston.createLogger(
level: 'info',
format: winston.format.json(),
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.log`.
//
new winston.transports.File( filename: 'error.log', level: 'error' ),
new winston.transports.File( filename: 'combined.log' )
]
);

//
// If we're not in production then log to the `console` with the format:
// `$info.level: $info.message JSON.stringify( ...rest ) `
//
if (process.env.NODE_ENV !== 'production')
logger.add(new winston.transports.Console(
format: winston.format.simple()
));


// Validate Server Configuration
if (envData.error)
winston.error('[APP-CONFIG]:', JSON.stringify(envData.error))
winston.error('[APP-CONFIG]:', 'Error on Server Configuration')
return


var test = function()
winston.error("ERROR log");
winston.info("INFO log");
winston.debug("DEBUG log");

test();


I am getting the following error,
Can someone please help fix my sample code?



enter image description here










share|improve this question























  • It will be great if you can describe the issue. Are you getting any error message? What is your expected output? What is your actual output?
    – yeya
    Nov 10 at 22:08










  • @yeya, updated the question with the error I'm facing.
    – Aniruddha Raje
    Nov 11 at 7:12











  • What is your node version? Run node -v
    – yeya
    Nov 11 at 7:39










  • I'm using v4.6.2
    – Aniruddha Raje
    Nov 11 at 7:39














up vote
0
down vote

favorite












I am trying to create a sample nodeJs program to use and understand winston library for nodeJs,

I had a sample code from one of the existing projects and I am trying to create a sample code sample,

The code contains winston configuration and a simple test function to print logs using different log levels.

I'm using node version 4.6.2, I know it's pretty old but, the existing application is using the same, therefore I wanted to understand the implementation on the same version,
The example consists of the following code and a log folder, in which I wish to print the log.



Following is a sample code which I am trying to run -



var winston = require('winston');
var envData = require('dotenv').config(path: 'server.env')
require('winston-daily-rotate-file');

var levelLog = 'debug';
var winstonTransports = ;

const logDTFormat = () => (new Date().toFormat('DD MMM YYYY HH24:MI:SS'));

// Winston Rotate File Logs
var transportDailyRotate = new (winston.transports.DailyRotateFile)(
filename: './logs/hrfid_app_',
datePattern: 'yyyy-MM-dd.log',
prepend: false,
level: levelLog,
timestamp: function()
var dateTime = new Date(Date.now());
return dateTime.toFormat('DD/MM/YYYY HH24:MI:SS');

);

winstonTransports.push(transportDailyRotate);

// Winston Console Log
var trasportConsole = new (winston.transports.Console)(
timestamp: logDTFormat,
colorize: true,
level: levelLog
);

winstonTransports.push(trasportConsole);

// Winston Config
winston.configure(
level: levelLog,
transports: winstonTransports
);

const logger = winston.createLogger(
level: 'info',
format: winston.format.json(),
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.log`.
//
new winston.transports.File( filename: 'error.log', level: 'error' ),
new winston.transports.File( filename: 'combined.log' )
]
);

//
// If we're not in production then log to the `console` with the format:
// `$info.level: $info.message JSON.stringify( ...rest ) `
//
if (process.env.NODE_ENV !== 'production')
logger.add(new winston.transports.Console(
format: winston.format.simple()
));


// Validate Server Configuration
if (envData.error)
winston.error('[APP-CONFIG]:', JSON.stringify(envData.error))
winston.error('[APP-CONFIG]:', 'Error on Server Configuration')
return


var test = function()
winston.error("ERROR log");
winston.info("INFO log");
winston.debug("DEBUG log");

test();


I am getting the following error,
Can someone please help fix my sample code?



enter image description here










share|improve this question























  • It will be great if you can describe the issue. Are you getting any error message? What is your expected output? What is your actual output?
    – yeya
    Nov 10 at 22:08










  • @yeya, updated the question with the error I'm facing.
    – Aniruddha Raje
    Nov 11 at 7:12











  • What is your node version? Run node -v
    – yeya
    Nov 11 at 7:39










  • I'm using v4.6.2
    – Aniruddha Raje
    Nov 11 at 7:39












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to create a sample nodeJs program to use and understand winston library for nodeJs,

I had a sample code from one of the existing projects and I am trying to create a sample code sample,

The code contains winston configuration and a simple test function to print logs using different log levels.

I'm using node version 4.6.2, I know it's pretty old but, the existing application is using the same, therefore I wanted to understand the implementation on the same version,
The example consists of the following code and a log folder, in which I wish to print the log.



Following is a sample code which I am trying to run -



var winston = require('winston');
var envData = require('dotenv').config(path: 'server.env')
require('winston-daily-rotate-file');

var levelLog = 'debug';
var winstonTransports = ;

const logDTFormat = () => (new Date().toFormat('DD MMM YYYY HH24:MI:SS'));

// Winston Rotate File Logs
var transportDailyRotate = new (winston.transports.DailyRotateFile)(
filename: './logs/hrfid_app_',
datePattern: 'yyyy-MM-dd.log',
prepend: false,
level: levelLog,
timestamp: function()
var dateTime = new Date(Date.now());
return dateTime.toFormat('DD/MM/YYYY HH24:MI:SS');

);

winstonTransports.push(transportDailyRotate);

// Winston Console Log
var trasportConsole = new (winston.transports.Console)(
timestamp: logDTFormat,
colorize: true,
level: levelLog
);

winstonTransports.push(trasportConsole);

// Winston Config
winston.configure(
level: levelLog,
transports: winstonTransports
);

const logger = winston.createLogger(
level: 'info',
format: winston.format.json(),
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.log`.
//
new winston.transports.File( filename: 'error.log', level: 'error' ),
new winston.transports.File( filename: 'combined.log' )
]
);

//
// If we're not in production then log to the `console` with the format:
// `$info.level: $info.message JSON.stringify( ...rest ) `
//
if (process.env.NODE_ENV !== 'production')
logger.add(new winston.transports.Console(
format: winston.format.simple()
));


// Validate Server Configuration
if (envData.error)
winston.error('[APP-CONFIG]:', JSON.stringify(envData.error))
winston.error('[APP-CONFIG]:', 'Error on Server Configuration')
return


var test = function()
winston.error("ERROR log");
winston.info("INFO log");
winston.debug("DEBUG log");

test();


I am getting the following error,
Can someone please help fix my sample code?



enter image description here










share|improve this question















I am trying to create a sample nodeJs program to use and understand winston library for nodeJs,

I had a sample code from one of the existing projects and I am trying to create a sample code sample,

The code contains winston configuration and a simple test function to print logs using different log levels.

I'm using node version 4.6.2, I know it's pretty old but, the existing application is using the same, therefore I wanted to understand the implementation on the same version,
The example consists of the following code and a log folder, in which I wish to print the log.



Following is a sample code which I am trying to run -



var winston = require('winston');
var envData = require('dotenv').config(path: 'server.env')
require('winston-daily-rotate-file');

var levelLog = 'debug';
var winstonTransports = ;

const logDTFormat = () => (new Date().toFormat('DD MMM YYYY HH24:MI:SS'));

// Winston Rotate File Logs
var transportDailyRotate = new (winston.transports.DailyRotateFile)(
filename: './logs/hrfid_app_',
datePattern: 'yyyy-MM-dd.log',
prepend: false,
level: levelLog,
timestamp: function()
var dateTime = new Date(Date.now());
return dateTime.toFormat('DD/MM/YYYY HH24:MI:SS');

);

winstonTransports.push(transportDailyRotate);

// Winston Console Log
var trasportConsole = new (winston.transports.Console)(
timestamp: logDTFormat,
colorize: true,
level: levelLog
);

winstonTransports.push(trasportConsole);

// Winston Config
winston.configure(
level: levelLog,
transports: winstonTransports
);

const logger = winston.createLogger(
level: 'info',
format: winston.format.json(),
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.log`.
//
new winston.transports.File( filename: 'error.log', level: 'error' ),
new winston.transports.File( filename: 'combined.log' )
]
);

//
// If we're not in production then log to the `console` with the format:
// `$info.level: $info.message JSON.stringify( ...rest ) `
//
if (process.env.NODE_ENV !== 'production')
logger.add(new winston.transports.Console(
format: winston.format.simple()
));


// Validate Server Configuration
if (envData.error)
winston.error('[APP-CONFIG]:', JSON.stringify(envData.error))
winston.error('[APP-CONFIG]:', 'Error on Server Configuration')
return


var test = function()
winston.error("ERROR log");
winston.info("INFO log");
winston.debug("DEBUG log");

test();


I am getting the following error,
Can someone please help fix my sample code?



enter image description here







node.js winston






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 7:53

























asked Nov 10 at 20:57









Aniruddha Raje

4881228




4881228











  • It will be great if you can describe the issue. Are you getting any error message? What is your expected output? What is your actual output?
    – yeya
    Nov 10 at 22:08










  • @yeya, updated the question with the error I'm facing.
    – Aniruddha Raje
    Nov 11 at 7:12











  • What is your node version? Run node -v
    – yeya
    Nov 11 at 7:39










  • I'm using v4.6.2
    – Aniruddha Raje
    Nov 11 at 7:39
















  • It will be great if you can describe the issue. Are you getting any error message? What is your expected output? What is your actual output?
    – yeya
    Nov 10 at 22:08










  • @yeya, updated the question with the error I'm facing.
    – Aniruddha Raje
    Nov 11 at 7:12











  • What is your node version? Run node -v
    – yeya
    Nov 11 at 7:39










  • I'm using v4.6.2
    – Aniruddha Raje
    Nov 11 at 7:39















It will be great if you can describe the issue. Are you getting any error message? What is your expected output? What is your actual output?
– yeya
Nov 10 at 22:08




It will be great if you can describe the issue. Are you getting any error message? What is your expected output? What is your actual output?
– yeya
Nov 10 at 22:08












@yeya, updated the question with the error I'm facing.
– Aniruddha Raje
Nov 11 at 7:12





@yeya, updated the question with the error I'm facing.
– Aniruddha Raje
Nov 11 at 7:12













What is your node version? Run node -v
– yeya
Nov 11 at 7:39




What is your node version? Run node -v
– yeya
Nov 11 at 7:39












I'm using v4.6.2
– Aniruddha Raje
Nov 11 at 7:39




I'm using v4.6.2
– Aniruddha Raje
Nov 11 at 7:39












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Upgrade to node 6 or 8.



Latest winston require at least node 6.4



https://github.com/winstonjs/winston/blob/c42ab7fdc51b88db180a7dd90c52ce04ddd4e054/package.json#L69



Or use an older version of winston by deleting the existing one and execute npm install winston@2.4.4






share|improve this answer






















  • actually the existing application runs on v4.6.2, that is why I am trying to do the same :/
    – Aniruddha Raje
    Nov 11 at 7:59










  • It's OK to try, but it won't work. You will need to rewrite winston library for this. Try to upgrade, maybe your existing application will work well on 8 too. Anyway you can use nvm to use multiple node versions on the same machine.
    – yeya
    Nov 11 at 8:05










  • yes I'm using nvm, the code runs on v10+, but the project dependency is on v4.6.2 and the logs are working in the project, why not on this simple code :/
    – Aniruddha Raje
    Nov 11 at 8:07






  • 1




    You can't use this winston library with node 4. It uses es6 syntax that not supported in node 4. You can downgrade to winston 2 instead of 3, that will give you support to node 4.
    – yeya
    Nov 11 at 8:12










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243345%2fhow-to-use-winston-in-nodejs%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Upgrade to node 6 or 8.



Latest winston require at least node 6.4



https://github.com/winstonjs/winston/blob/c42ab7fdc51b88db180a7dd90c52ce04ddd4e054/package.json#L69



Or use an older version of winston by deleting the existing one and execute npm install winston@2.4.4






share|improve this answer






















  • actually the existing application runs on v4.6.2, that is why I am trying to do the same :/
    – Aniruddha Raje
    Nov 11 at 7:59










  • It's OK to try, but it won't work. You will need to rewrite winston library for this. Try to upgrade, maybe your existing application will work well on 8 too. Anyway you can use nvm to use multiple node versions on the same machine.
    – yeya
    Nov 11 at 8:05










  • yes I'm using nvm, the code runs on v10+, but the project dependency is on v4.6.2 and the logs are working in the project, why not on this simple code :/
    – Aniruddha Raje
    Nov 11 at 8:07






  • 1




    You can't use this winston library with node 4. It uses es6 syntax that not supported in node 4. You can downgrade to winston 2 instead of 3, that will give you support to node 4.
    – yeya
    Nov 11 at 8:12














up vote
1
down vote



accepted










Upgrade to node 6 or 8.



Latest winston require at least node 6.4



https://github.com/winstonjs/winston/blob/c42ab7fdc51b88db180a7dd90c52ce04ddd4e054/package.json#L69



Or use an older version of winston by deleting the existing one and execute npm install winston@2.4.4






share|improve this answer






















  • actually the existing application runs on v4.6.2, that is why I am trying to do the same :/
    – Aniruddha Raje
    Nov 11 at 7:59










  • It's OK to try, but it won't work. You will need to rewrite winston library for this. Try to upgrade, maybe your existing application will work well on 8 too. Anyway you can use nvm to use multiple node versions on the same machine.
    – yeya
    Nov 11 at 8:05










  • yes I'm using nvm, the code runs on v10+, but the project dependency is on v4.6.2 and the logs are working in the project, why not on this simple code :/
    – Aniruddha Raje
    Nov 11 at 8:07






  • 1




    You can't use this winston library with node 4. It uses es6 syntax that not supported in node 4. You can downgrade to winston 2 instead of 3, that will give you support to node 4.
    – yeya
    Nov 11 at 8:12












up vote
1
down vote



accepted







up vote
1
down vote



accepted






Upgrade to node 6 or 8.



Latest winston require at least node 6.4



https://github.com/winstonjs/winston/blob/c42ab7fdc51b88db180a7dd90c52ce04ddd4e054/package.json#L69



Or use an older version of winston by deleting the existing one and execute npm install winston@2.4.4






share|improve this answer














Upgrade to node 6 or 8.



Latest winston require at least node 6.4



https://github.com/winstonjs/winston/blob/c42ab7fdc51b88db180a7dd90c52ce04ddd4e054/package.json#L69



Or use an older version of winston by deleting the existing one and execute npm install winston@2.4.4







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 10:08

























answered Nov 11 at 7:58









yeya

469514




469514











  • actually the existing application runs on v4.6.2, that is why I am trying to do the same :/
    – Aniruddha Raje
    Nov 11 at 7:59










  • It's OK to try, but it won't work. You will need to rewrite winston library for this. Try to upgrade, maybe your existing application will work well on 8 too. Anyway you can use nvm to use multiple node versions on the same machine.
    – yeya
    Nov 11 at 8:05










  • yes I'm using nvm, the code runs on v10+, but the project dependency is on v4.6.2 and the logs are working in the project, why not on this simple code :/
    – Aniruddha Raje
    Nov 11 at 8:07






  • 1




    You can't use this winston library with node 4. It uses es6 syntax that not supported in node 4. You can downgrade to winston 2 instead of 3, that will give you support to node 4.
    – yeya
    Nov 11 at 8:12
















  • actually the existing application runs on v4.6.2, that is why I am trying to do the same :/
    – Aniruddha Raje
    Nov 11 at 7:59










  • It's OK to try, but it won't work. You will need to rewrite winston library for this. Try to upgrade, maybe your existing application will work well on 8 too. Anyway you can use nvm to use multiple node versions on the same machine.
    – yeya
    Nov 11 at 8:05










  • yes I'm using nvm, the code runs on v10+, but the project dependency is on v4.6.2 and the logs are working in the project, why not on this simple code :/
    – Aniruddha Raje
    Nov 11 at 8:07






  • 1




    You can't use this winston library with node 4. It uses es6 syntax that not supported in node 4. You can downgrade to winston 2 instead of 3, that will give you support to node 4.
    – yeya
    Nov 11 at 8:12















actually the existing application runs on v4.6.2, that is why I am trying to do the same :/
– Aniruddha Raje
Nov 11 at 7:59




actually the existing application runs on v4.6.2, that is why I am trying to do the same :/
– Aniruddha Raje
Nov 11 at 7:59












It's OK to try, but it won't work. You will need to rewrite winston library for this. Try to upgrade, maybe your existing application will work well on 8 too. Anyway you can use nvm to use multiple node versions on the same machine.
– yeya
Nov 11 at 8:05




It's OK to try, but it won't work. You will need to rewrite winston library for this. Try to upgrade, maybe your existing application will work well on 8 too. Anyway you can use nvm to use multiple node versions on the same machine.
– yeya
Nov 11 at 8:05












yes I'm using nvm, the code runs on v10+, but the project dependency is on v4.6.2 and the logs are working in the project, why not on this simple code :/
– Aniruddha Raje
Nov 11 at 8:07




yes I'm using nvm, the code runs on v10+, but the project dependency is on v4.6.2 and the logs are working in the project, why not on this simple code :/
– Aniruddha Raje
Nov 11 at 8:07




1




1




You can't use this winston library with node 4. It uses es6 syntax that not supported in node 4. You can downgrade to winston 2 instead of 3, that will give you support to node 4.
– yeya
Nov 11 at 8:12




You can't use this winston library with node 4. It uses es6 syntax that not supported in node 4. You can downgrade to winston 2 instead of 3, that will give you support to node 4.
– yeya
Nov 11 at 8:12

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243345%2fhow-to-use-winston-in-nodejs%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo