Loggly Subdomain is required - node.js

I'm trying to implement to my NodeJs API a logging system using:
Loggly
Winston
Morgan
When I run my server I got the error:
Error: Loggly Subdomain is required
But the subdomain is defined as follows
What I was trying to do to put the Loggly config inside of a module:
module.exports = {
loggly: {
token: process.env.LOG_TOKEN,
subdomain: process.env.SUBDOMAIN,
tags: ["Winston-NodeJS"],
json: true
}
};
Using also the ENV which are defined and contains the right info.
Then I created a new file called logger.js
// Requiring libs Loggly & Winston
const Loggly = require("winston-loggly-bulk").Loggly;
const winston = require("winston");
// Loggly config
const config = require("../config/config");
// Creating the logging
const logger = winston.createLogger({
transports: [
new Loggly(config.loggly), ==> Here the error occur!
new winston.transports.Console({ level: "info" })
]
});
// Logging stream
logger.stream = {
write: (info) => {
logger.info(info);
}
};
module.exports = logger;
In this script, the error occurs when I call new Loggly(...) seems cannot read my SUBDOMAIN and I cannot understand a different way of doing as it is the first time I'm trying this implementation.

Put this line require("dotenv").config(); on line 1 in server.js.

Related

How to store nodejs server runtime error logs in file winston nodejs

I am new to winston and trying to store nodejs server logs into file.
const {
createLogger,
transports,
format
} = require('winston');
const logger = createLogger({
transports: [
new transports.File({
filename : 'info.log',
level: 'info',
format: format.combine(format.timestamp(), format.json())
})
]
})
module.exports = logger;
And I can store log in the file using logger.log('info',"Inside /login")
How can I store the nodejs server logs which are generated automatically run time when error happens.
For example this log which I can see in console when the mySQL db connection was closed and these logs were generated by nodejs server.

winston 3.2.1 version is not creating log file

I tried to create winston(logger file) using node js.winston 2.4.0 version its sucessfully storeing info and error in log file.but latest version its created log file but cant stored error and info messages.How to fix it
Winston.js
var winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
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()
}));
}
app.js
/**
* #fileoverview Root file of the application.
*/
// Import ExpressJS
const winston = require('winston');
const express = require('express');
const app = express();
// StartUp Processes
require('./startup/logging_error_startup');
require('./startup/routes_startup')(app);
require('./startup/check_security_key_startup')();
require('./startup/validation_startup')();
require('./startup/swagger_startup')(app);
require('./startup/production_startup')(app);
// Start the server
const port = process.env.PORT || 4202;
app.listen(port, () => winston.info(`Server Started and Listening on port ${port}`));
I got the output
winston] Attempt to write logs with no transports {"message":"Server Started and Listening on port 4202","level":"info"}
You need to import the logger object from Winston.js file into app.js. In your app.js code, the Winston import is for the npm package and not for the logger object created in Winston.js.
In Winston.js, export the logger object:
var winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
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()
}));
}
module.exports = logger;
And in app.js, use that logger object after importing.
const logger = require('./Winston'); //assuming Winston.js is in the same folder level as app.js
const express = require('express');
const app = express();
// StartUp Processes
require('./startup/logging_error_startup');
require('./startup/routes_startup')(app);
require('./startup/check_security_key_startup')();
require('./startup/validation_startup')();
require('./startup/swagger_startup')(app);
require('./startup/production_startup')(app);
// Start the server
const port = process.env.PORT || 4202;
app.listen(port, () => logger.info(`Server Started and Listening on port ${port}`));
Alternatively you can modify the default logger in your winston.js with
var winston = require('winston');
const newLogger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [.........
winston.add(newLogger);
instead of
var winston = require('winston');
const logger = winston.createLogger({....
module.exports = logger
and import the default logger in each file with
const logger = require('winston')
logger.info('log');
without dealing with relative paths each time you import the logger to different files.

Winston logging in console but not in the log files

I'm using Winston for logging in my project. I can see the logs in the console but there is no logging inside the log files.
Winston.js file
var appRoot = require('app-root-path');
var winston = require('winston');
const {transports, createLogger, format} = require('winston');
winston.addColors( winston.config.npm.colors );
const logger = winston.createLogger({
level: 'info',
format: format.combine(
format.timestamp({format:'MM-YY-DD hh:mm:ss a'}),
format.json(),
),
transports: [
new winston.transports.File({ filename: './logs/error.log', level: 'error' }),
new winston.transports.File({ handleExceptions: true,colorize:true,
json: true,filename: './logs/app.log',
})
]
});
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.simple()
}));
}
module.exports = logger;
Server.js code
var morgan = require('morgan');
var winston = require('./server/config/winston');
Node version
8.11.3
Winston version
3.1.0
You need to make sure 'logs' folder exists. Winston doesnt take care of non-existent folder, nor giving any warning or errors.
You can put following code in your server.js
var fs = require( 'fs' );
var logDir = 'logs';
if ( !fs.existsSync( logDir ) ) {
fs.mkdirSync( logDir );
}
winston.info('Hey NODE');
Using the absolute folder path inside the winston.js solved the problem.

Set default winston logger for all modules

I am trying to setup winston to work in all my modules in the same fashion as in here:
using winston in several modules
But I am running in problems.
I have setup a logger.js file to configure the console logger:
var winston = require('winston');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
timestamp: true,
level: 'verbose',
colorize: true
})
]
});
module.exports = logger;
I then require that logger in my main app.js file:
var logger = require('./logger.js');
logger.info('Starting server'); // this console log looks great, just as I configured
However when I try an require winston in al other modules I lose the config I setup for winston in the logger.js
Other Module:
var logger = require('winston');
logger.info('in another module'); // this is back to winstons default console transport
According to the link I referenced above I should be able to require winston in all other modules and the transports I defined/configured should still be the same. Only thing I can think is that this is not true for the Console transport. If not what gives? How do I configure the Console transport once and then use winston in all other modules?
For your current solution to work, you have to make sure you have one version of winston. You shouldn't have it installed once under your main app and another time under your other module. Then in here you are creating a new instance of logger and not using the default.
You should instead of above do this:
var winston = require('winston');
winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, {
timestamp: true,
level: 'verbose',
colorize: true
});
I think this should work. If that didn't work, you can try one of these methods:
Get them to use your ./logger module instead. This works great in internal modules that are part of the app codebase.
Or make your other module configurable. Do something like require('other-module').logger = require('./logger'); or require('other-module').setLogger(require('./logger'));. You can check this question if you want to know more about this method.

Where is nodejs log file?

I can't find a place where nodejs log file is stored.
Because in my node server I have "Segmentation fault", I want to look at log file for additional info...
There is no log file. Each node.js "app" is a separate entity. By default it will log errors to STDERR and output to STDOUT. You can change that when you run it from your shell to log to a file instead.
node my_app.js > my_app_log.log 2> my_app_err.log
Alternatively (recommended), you can add logging inside your application either manually or with one of the many log libraries:
winston
log4js
...
forever might be of interest to you. It will run your .js-File 24/7 with logging options. Here are two snippets from the help text:
[Long Running Process]
The forever process will continue to run outputting log messages to the console.
ex. forever -o out.log -e err.log my-script.js
and
[Daemon]
The forever process will run as a daemon which will make the target process start
in the background. This is extremely useful for remote starting simple node.js scripts
without using nohup. It is recommended to run start with -o -l, & -e.
ex. forever start -l forever.log -o out.log -e err.log my-daemon.js
forever stop my-daemon.js
If you use docker in your dev you can do this in another shell:
docker attach running_node_app_container_name
That will show you STDOUT and STDERR.
For nodejs log file you can use winston and morgan and in place of your console.log() statement user winston.log() or other winston methods to log.
For working with winston and morgan you need to install them using npm. Example:
npm i -S winston
npm i -S morgan
Then create a folder in your project with name winston and then create a config.js in that folder and copy this code given below.
const appRoot = require('app-root-path');
const winston = require('winston');
// define the custom settings for each transport (file, console)
const options = {
file: {
level: 'info',
filename: `${appRoot}/logs/app.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
},
console: {
level: 'debug',
handleExceptions: true,
json: false,
colorize: true,
},
};
// instantiate a new Winston Logger with the settings defined above
let logger;
if (process.env.logging === 'off') {
logger = winston.createLogger({
transports: [
new winston.transports.File(options.file),
],
exitOnError: false, // do not exit on handled exceptions
});
} else {
logger = winston.createLogger({
transports: [
new winston.transports.File(options.file),
new winston.transports.Console(options.console),
],
exitOnError: false, // do not exit on handled exceptions
});
}
// create a stream object with a 'write' function that will be used by `morgan`
logger.stream = {
write(message) {
logger.info(message);
},
};
module.exports = logger;
After copying the above code make make a folder with name logs parallel to winston or wherever you want and create a file app.log in that logs folder. Go back to config.js and set the path in the 5th line "filename: ${appRoot}/logs/app.log,
" to the respective app.log created by you.
After this go to your index.js and include the following code in it.
const morgan = require('morgan');
const winston = require('./winston/config');
const express = require('express');
const app = express();
app.use(morgan('combined', { stream: winston.stream }));
winston.info('You have successfully started working with winston and morgan');

Resources