NestJS Logging :: Enabling/disabling globally - node.js

I have a couple of utility classes that are called by the Controllers or Services in NestJS. Each utility class is declared with a logger, like so:
export class A {
private logger = new Logger(A.name);
test() {
this.logger.log('TEST');
}
}
This helps during development and while troubleshooting, but in production, I'd like to be able to turn the logs off for debug, verbose, and log levels.
I haven't been able to figure out a reliable way to do that globally, apart from commenting out the logging statements individually. Is there a way to do so from a central place like main.ts?

You can use a ternary in your app's main.ts file to set the log levels when you create your app:
const app = await NestFactory.create(AppModule, {
logger:
process.env.NODE_ENV === 'development' ? ['log', 'debug', 'error', 'verbose', 'warn'] : ['error', 'warn'],
})

Related

How to reference the app instance in a module in Nest.js

I'm working on a project that's using multiple Nest repos, around 4.
Every repo needs to implementing logging to log things like
Server lifecycle events
Uncaught errors
HTTP requests/responses
Ideally, I'd like to package everything up into a module which I can publish to my company's NPM organization and just consume directly in each of my projects.
That way, it would take very minimal code to get logging set up in each project.
One of the things I'd like to log in my server lifecycle event is the server's url.
I know you can get this via app.getUrl() in the bootstrapping phase, but it would be great to have access to the app instance in a module's lifecycle hook like so.
#Module({})
export class LoggingModule implements NestModule {
onApplicationBootstrap() {
console.log(`Server started on ${app.getUrl()}`)
}
beforeApplicationShutdown() {
console.log('shutting down')
}
onApplicationShutdown() {
console.log('successfully shut down')
}
configure(consumer: MiddlewareConsumer) {
consumer.apply(LoggingMiddleware).forRoutes('*')
}
}
Is this possible?
There's no way (besides hacky ones, maybe) to access the app itself inside modules.
As you can see here, app.getUrl() uses the underlying HTTP server. Thus I guess you can retrieve the same data using the provider HttpAdapterHost.
Ï thought I'd chime in and offer one of the hacky solutions. Only use this, if there is absolutely no other way or your deadline is coming in an hour.
Create a class that can hold the application instance
export class AppHost {
app: INesApplication
}
And a module to host it
#Module({
providers: [AppHost]
exports: [AppHost]
})
export class AppHostModule {}
In your bootstrap() function, retrieve the AppHost instance and assign the app itself
// after NestFactory.create() ...
app.select(AppHostModule).get(AppHost).app = app;
Now, the actual application will be available wherever you inject AppHost.
Be aware, though, that the app will not be available inside AppHost before the whole application bootstraps (in onModuleInit, onApplicationBootstrap hooks or in provider factories), but it should be available in shutdown hooks.
Not sure is that hacky... I'm using this to prevent the server from starting in case of pending migrations.
// AppModule.ts
export class AppModule implements NestModule {
app: INestApplication;
async configure(consumer: MiddlewareConsumer) {
if (await this.hasPendingMigrations()) {
setTimeout(()=> {
this.logger.error("There are pending migrations!")
process.exitCode = 1;
this.app.close();
}, 1000);
}
//...
}
public setApp(app: INestApplication) {
this.app = app;
}
//...
}
//main.ts
const app = await NestFactory.create(AppModule, {
logger: config.cfgServer.logger,
});
app.get(AppModule).setApp(app);

ostrio:logger no client logs

i have initialized the logger like in the docs: https://atmospherejs.com/ostrio/logger
with a transport to mongodb: https://atmospherejs.com/ostrio/loggermongo#initialization-isomorphic
import { Logger } from 'meteor/ostrio:logger';
import { LoggerMongo } from 'meteor/ostrio:loggermongo';
export const idLogger = new IdLogger();
export function addMongoDbLogging(idLogger, filters) {
(new LoggerMongo(idLogger , {
collection: AppLogs,
})).enable({
enable: true,
filter: filters,
client: true,
server: true,
});
}
The code lies is in the imports directory
And the call to is in the server directory during meteor startup.
if(Meteor.isServer) {
initLogger(); // calls addMongoDbLogging();
}
On The server logging works with no problems. All logs are written in the AppLogs Collection.
But when i try to log something from the client... nothing happens.
Same behavior is for other transports like console. See: https://atmospherejs.com/ostrio/loggerconsole
Serverside all logs show up in my console. Client logs are not shown on neither server or client consoles.
Can someone tell me what's wrong here?
Update: It could be that the server/startup is not the right place for isomorphic code. Where should this be put?
Ok i found the problem.
It was the location of the code.
It has to be done in the /lib folder.

How to split Nest.js microservices into separate projects?

Let's say I want to create a simplistic cinema-management platform. It needs few microservices: movies, cinemas, payments, etc.
How would you go about doing it in Nest.js? I don't want them in the same big folder as that feels like making a monolith. I want them to be separate Nest.js projects with their own git repositories so I can orchestrate them with Kubernetes later on.
How? How to connect from service cinemas to service movies if they are two separate projects and only share, let's say, Redis?
Edit:
This is not a question about microservices in general. This is a question Nest.js specific. I read the documentation, I know there are decorators like #Client for connecting to the transport layer. I just want to know where to use that decorator and maybe see a short snippet of code on "having two separate Nest.js repositories how to connect them together so they can talk to each other".
I don't care about the transport layer, that thing I can figure out myself. I just need some advice on the framework itself as I believe the documentation is lacking.
I got it working. Basically the way to do it is to create two separate projects. Let's say - one is a createMicroservice and another is just an HTTP app (but could easily be another microservice). I used a "normal" app just so I can call it easily for testing.
Here is the main.ts file that creates microservice.
import { NestFactory } from '#nestjs/core';
import { AppModule } from './app.module';
import { Transport } from '#nestjs/common/enums/transport.enum';
async function bootstrap() {
const app = await NestFactory.createMicroservice(AppModule, {
transport: Transport.REDIS,
options: {
url: 'redis://localhost:6379',
},
});
await app.listen(() => console.log('MoviesService is running.'));
}
bootstrap();
And one of the controllers:
#Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
#MessagePattern({ cmd: 'LIST_MOVIES' })
listMovies(): string[] {
return ['Pulp Fiction', 'Blade Runner', 'Hatred'];
}
}
Now - in the microservice you declare to what kinds of events should controllers react to (#MessagePattern). While in the "normal" service you do this in the controller when you want to ask other microservices for something (the main.ts is the simplest example that you get when you create a new project using #nestjs/cli.
The controller code:
#Controller()
export class AppController {
private readonly client: ClientProxy;
constructor(private readonly appService: AppService) {
this.client = ClientProxyFactory.create({
transport: Transport.REDIS,
options: {
url: 'redis://localhost:6379',
},
});
}
#Get()
listMovies() {
const pattern = { cmd: 'LIST_MOVIES' };
return this.client.send<string[]>(pattern, []);
}
}
So as long a client is connected to the same transport layer as the microservice - they can talk to each other by using the #MessagePattern.
For nicer code you can move the this.client part from a constructor to a provider and then use dependency injection by declaring the provider in the module.

Enable GA and other trackers only for production when assets are prebuilt with webpack

I'm shipping frontend application I want to track stats for (GA and other trackers). The app is prebuilt with webpack on CI and I'd like to have the same bundle to be shipped to both staging and prod environments.
But there is an issue with that: if I enable trackers during build time they'll be enabled on both envs.
One way I see is to add set window.enableTrackers = true flag in index.html when sending app code from server and then decide whether to install trackers or not, but it seems to be pretty ugly solution.
Is there any other way u use to do it? Checking domain name on the client maybe? Something like:
if(document.location.hostname === 'prod.example.com') {
// enable trackers
}
This would be best resolved at the time the compilation is made by configuring Webpack differently for the production and stage bundles, where you could use the CLI --define or --env to explicitly enable tracking.
If you don't resolve the inclusion of tracking during the compilation, then you will have to resort to other means. One option is to evaluate during runtime whether the GA script should be loaded in the document. Here's an example of a Rect element that will load the script only if the hostname matches a configurable regular expression:
import React, {Component, PropTypes} from 'react'
const production = new RegExp(RE_PRODUCTION_HOST)
export const ga = window.ga = window.ga || function () {
(ga.q = ga.q || []).push(arguments)
}
export class Analytics extends Component {
static propTypes = {
id: PropTypes.string.isRequired
};
componentDidMount () {
// Runtime evaluation for loading the GA script
if (!self.location.hostname.match(production)) {
return
}
const script = document.createElement('script')
script.src = 'https://www.google-analytics.com/analytics.js'
script.async = true
document.body.appendChild(script)
}
render () {
ga.l = +new Date()
ga('create', this.props.id, 'auto')
ga('send', 'pageview')
return null
}
}
Here, I can still safely define RE_PRODUCTION_HOST to be configured to production.com or even ^(www.)?production.com% and I can change it during test compilations to make assertions.
Another option is to add filters in Google Analytics that will include/exclude results depending on the hostname. This won't require you to make any further changes in your code.

Node.js Logging

Is there any library which will help me to handle logging in my Node.Js application? All I want to do is, I want to write all logs into a File and also I need an options like rolling out the file after certain size or date.
I have incorporated log4js im trying to keep all the configuration details in one file and use only the methods in other application files for ease of maintenance. But it doesnt work as expected. Here is what I'm trying to do
var log4js = require('log4js');
log4js.clearAppenders()
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('test.log'), 'test');
var logger = log4js.getLogger('test');
logger.setLevel('ERROR');
var traceLogger = function (message) {
logger.trace('message');
};
var errorLogger = function (message) {
logger.trace(message);
};
exports.trace = traceLogger;
exports.error = errorLogger;
I have included this file in other files and tried
log.error ("Hello Error Message");
But it is not working. Is there anything wrong in this ?
Winston is a pretty good logging library. You can write logs out to a file using it.
Code would look something like:
var winston = require('winston');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ json: false, timestamp: true }),
new winston.transports.File({ filename: __dirname + '/debug.log', json: false })
],
exceptionHandlers: [
new (winston.transports.Console)({ json: false, timestamp: true }),
new winston.transports.File({ filename: __dirname + '/exceptions.log', json: false })
],
exitOnError: false
});
module.exports = logger;
You can then use this like:
var logger = require('./log');
logger.info('log to file');
Scribe.JS Lightweight Logger
I have looked through many loggers, and I wasn't able to find a lightweight solution - so I decided to make a simple solution that is posted on github.
Saves the file which are organized by user, date, and level
Gives you a pretty output (we all love that)
Easy-to-use HTML interface
I hope this helps you out.
Online Demo
http://bluejamesbond.github.io/Scribe.js/
Secure Web Access to Logs
Prints Pretty Text to Console Too!
Web Access
Github
https://github.com/bluejamesbond/Scribe.js
Log4js is one of the most popular logging library for nodejs application.
It supports many cool features:
Coloured console logging
Replacement of node's console.log functions (optional)
File appender, with log rolling based on file size
SMTP, GELF, hook.io, Loggly appender
Multiprocess appender (useful when you've got worker processes)
A logger for connect/express servers
Configurable log message layout/patterns
Different log levels for different log categories (make some parts
of your app log as DEBUG, others only ERRORS, etc.)
Example:
Installation: npm install log4js
Configuration (./config/log4js.json):
{"appenders": [
{
"type": "console",
"layout": {
"type": "pattern",
"pattern": "%m"
},
"category": "app"
},{
"category": "test-file-appender",
"type": "file",
"filename": "log_file.log",
"maxLogSize": 10240,
"backups": 3,
"layout": {
"type": "pattern",
"pattern": "%d{dd/MM hh:mm} %-5p %m"
}
}
],
"replaceConsole": true }
Usage:
var log4js = require( "log4js" );
log4js.configure( "./config/log4js.json" );
var logger = log4js.getLogger( "test-file-appender" );
// log4js.getLogger("app") will return logger that prints log to the console
logger.debug("Hello log4js");// store log in file
You can also use npmlog by issacs, recommended in
https://npmjs.org/doc/coding-style.html.
You can find this module here
https://github.com/isaacs/npmlog
The "logger.setLevel('ERROR');" is causing the problem. I do not understand why, but when I set it to anything other than "ALL", nothing gets printed in the file. I poked around a little bit and modified your code. It is working fine for me. I created two files.
logger.js
var log4js = require('log4js');
log4js.clearAppenders()
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('test.log'), 'test');
var logger = log4js.getLogger('test');
logger.setLevel('ERROR');
var getLogger = function() {
return logger;
};
exports.logger = getLogger();
logger.test.js
var logger = require('./logger.js')
var log = logger.logger;
log.error("ERROR message");
log.trace("TRACE message");
When I run "node logger.test.js", I see only "ERROR message" in test.log file. If I change the level to "TRACE" then both lines are printed on test.log.
Winston is strong choice for most of the developers. I have been using winston for long. Recently I used winston with with papertrail which takes the application logging to next level.
Here is a nice screenshot from their site.
How its useful
you can manage logs from different systems at one place. this can be very useful when you have two backend communicating and can see logs from both at on place.
Logs are live. you can see realtime logs of your production server.
Powerful search and filter
you can create alerts to send you email if it encounters specific text in log.
and you can find more http://help.papertrailapp.com/kb/how-it-works/event-viewer/
A simple configuration using winston,winston-express and winston-papertrail node modules.
import winston from 'winston';
import expressWinston from 'express-winston';
//
// Requiring `winston-papertrail` will expose
// `winston.transports.Papertrail`
//
require('winston-papertrail').Papertrail;
// create winston transport for Papertrail
var winstonPapertrail = new winston.transports.Papertrail({
host: 'logsX.papertrailapp.com',
port: XXXXX
});
app.use(expressWinston.logger({
transports: [winstonPapertrail],
meta: true, // optional: control whether you want to log the meta data about the request (default to true)
msg: "HTTP {{req.method}} {{req.url}}", // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}"
expressFormat: true, // Use the default Express/morgan request formatting. Enabling this will override any msg if true. Will only output colors with colorize set to true
colorize: true, // Color the text and status code, using the Express/morgan color palette (text: gray, status: default green, 3XX cyan, 4XX yellow, 5XX red).
ignoreRoute: function (req, res) { return false; } // optional: allows to skip some log messages based on request and/or response
}));
A 'nodejslogger' module can be used for simple logging. It has three levels of logging (INFO, ERROR, DEBUG)
var logger = require('nodejslogger')
logger.init({"file":"output-file", "mode":"DIE"})
D : Debug, I : Info, E : Error
logger.debug("Debug logs")
logger.info("Info logs")
logger.error("Error logs")
The module can be accessed at : https://www.npmjs.com/package/nodejslogger
Observe that errorLogger is a wrapper around logger.trace. But the level of logger is ERROR so logger.trace will not log its message to logger's appenders.
The fix is to change logger.trace to logger.error in the body of errorLogger.
Each answer is 5 6 years old, so bit outdated or depreciated. Let's talk in 2020.
simple-node-logger is simple multi-level logger for console, file, and rolling file appenders. Features include:
levels: trace, debug, info, warn, error and fatal levels (plus all and off)
flexible appender/formatters with default to HH:mm:ss.SSS LEVEL message
add appenders to send output to console, file, rolling file, etc
change log levels on the fly
domain and category columns
overridable format methods in base appender
stats that track counts of all log statements including warn, error, etc
You can easily use it in any nodejs web application:
// create a stdout console logger
const log = require('simple-node-logger').createSimpleLogger();
or
// create a stdout and file logger
const log = require('simple-node-logger').createSimpleLogger('project.log');
or
// create a custom timestamp format for log statements
const SimpleNodeLogger = require('simple-node-logger'),
opts = {
logFilePath:'mylogfile.log',
timestampFormat:'YYYY-MM-DD HH:mm:ss.SSS'
},
log = SimpleNodeLogger.createSimpleLogger( opts );
or
// create a file only file logger
const log = require('simple-node-logger').createSimpleFileLogger('project.log');
or
// create a rolling file logger based on date/time that fires process events
const opts = {
errorEventName:'error',
logDirectory:'/mylogfiles', // NOTE: folder must exist and be writable...
fileNamePattern:'roll-<DATE>.log',
dateFormat:'YYYY.MM.DD'
};
const log = require('simple-node-logger').createRollingFileLogger( opts );
Messages can be logged by
log.info('this is logged info message')
log.warn('this is logged warn message')//etc..
PLUS POINT: It can send logs to console or socket. You can also append to log levels.
This is the most effective and easy way to handle logs functionality.
Here is lightweight module for logging data with full stack trace
#grdon/logger
const logger = require('#grdon/logger')({
defaultLogDirectory : __dirname + "/logs",
})
// ...
logger(someParams, 'logfile.txt')
logger(anotherParams, 'anotherLogFile.log')

Resources