Logger in Node Js Express Application

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello this is boshar in this tutorial we are going to implement logging functionality in express application here we have our repository and you can find the related changes right here we are going to start from the login start and the tutorial content can be found right here in the login final so the project is already opened right here let's start the implementation we are going to use a library for this one and that library is this winston so let's install winston first we are going to run npm install winston now we are going to create a blogger and let's create this one under src let's call this one as logger js and here we are going to use the dependency we just installed installed winston and from winston we are going to take a specific function and that is create logger and we are going to have a logger right here and we are just going to create logger and let's export this logger from this module and let's use this right here in the index.js here we have this console.log but instead of console.log we are going to use this logger so first let's get the logger and it is under src logger and here instead of running console.log we are going to replace this one with logger and it has the method about the log levels and let's go with the info it has levels like the error warn info the debug cd and variables and let's save this one and let's run the application so clearing the console and let's run npm start now in the console we have logs for the database part but the logs we are printing right here is actually at this line here we have this winston attempt to write logs with no transport and here it is saying the log bevel is info and the message is this one to make this part easier let's disable the database logs for now so here in the database js we have this logging and it was looking to the configuration based on the environment and that configuration is coming from this part we can either update the the config files or directly we can set logging as false so let's continue with this one now this is only printing this part now let's focus to this message right here so we have this logger instance and currently it doesn't have any transport option transport is basically telling winston where this looks to be shown and let's set a transport we do that right here in the create logger function we pass and options to this one and it is looking for an transports array and in this array we can define our transport and first we get the transports from winston then we call new transports and we go with the console so this is going to be printing all the logs to console so let's save this one now that message is gone so the application is restarted here we are just seeing this level and the the message now let's focus to the log levels the logger has different log levels so we can just print those logs right here so from the highest priority to lowest priority the logger has this error and let's print just the error and logger has one level let's say this is worn and logger has info info and blocker has variables and locker has debug and the last one is logger has this silly silly level of logs so if we save this one now the application is restarting again and here this is the app is running we are printing right here info level of log and we are also having these logs but only these three of them is printed this is the highest priority this is the lowest priority of the logs so just three of them is printed and that is because by default this logger the logger instance we created has the level as info so this info level is the default level therefore it is printing only the logs having this level and the higher priority ones so we can change the log level right here in the createlogger instance so we can say the level is going to let's say debug so if we save this one now it is printing the logs from error to debug right here and if we say silly it's going to be printing all the logs so we can dynamically change the the log levels and in the run time we can start with the debug and then we can switch to info level like that or we can use this configuration for the profiles for the different environments we can set different levels for the different profiles and just like we do that right here in the database configuration we can just get the configuration corresponding configuration file and use that value right here for the level we can also have multiple transports so it's not just going to print the logs to the console but we can also ask it to print the logs to the file so we can add a new transport and this is going to be file but this one is looking for an options and in this options it is looking for file name and let's say the file name will be app log so let's save this one now a file right here applock is created and if we check that file here we are seeing the messages printed right here is also written into this file so we can ask this winston to print the logs both the console and the file and we can also say when you are printing logs to a console let's set the level as let's call it the silly but if you are writing it to a file then let's use the level as info so let's save this now this debug is not applied the levels for each of these transports are applied so in console we see all log levels from silly to error but if we check the app lock right here it is printing error warn and info by the way this logger is appending all new logs to the existing file and it also has the capability to handle the file rotation so if this exits a specific limit it creates a new file and renames the old one we can create a custom format for our logs currently we go with the default implementation but we can set a format so we can just get the format function in this create logger we can set the format is going to be like this format this one we are calling this formats print f function and this function is having a callback function this function will be called with the object let's call this one as info basically it is having this object this one is object so here we can format the text so we can go like this let's use string literals so we can have info and this info has the the level level is this part this info or this silly debug variables so we print the level and we add dash then we print let's say info message saving this by the way i forget a comma here just this is an object and we have different keys and values so this is ending right here so save this one now here the logs are printed like this this is the level and this is the corresponding message so there is a dash between these level and the logs we can enhance the the visibility of this one so like we can add square brackets around this level and then we can add fixed uh fixed spacing like um like this one we can use this this is a text the level is a string so we can use pad and functionality of that one and let's say all of them will have seven characters long so it's printed like this also we can just go with the uppercase for the level so the log format is like this and also we can add a timestamp to this log so for that we can let's let's type it right here let's say we will have a custom format and now we are going to combine format combined and this is taking formats and one of the format we are looking for is timestamp so this is going to be adding timestamp to the to the to the info object and the next part is this one actually just cutting it and pasting here so we are combining this timestamp with this format now this timestamp is part of this info so we can add timestamp at the very beginning like this one info.timestamp and let's add spacing between this timestamp and the the log level now we are going to use this custom format as this format we are not using the level so let's get rid of that one and saving it now we have this time stamp right here and the the log level and corresponding message and we also see the same pattern right here in the file output so we can play with this logger functionality and create our own log style for different log environments and we can also use this logger in the database previously the database was just printing console logs but we can use the logger so let's get the logger here it is under under src folder so we are going just one directory up now we are going to set this logging this is sqlize functionalities and it is allowing us to set instead of a boolean right here it's allowing us to set a function so like this this function will be called with the let's say the message the message it's going to be printed so we are able to take the message and just we can use these loggers like info level and print this message so saving this one and right here we are also seeing the blocks generated by sqlize and also we are seeing our own application logs right here so that's all for the winston logger functionality thanks for watching and hope to see you in the next tutorials
Info
Channel: Programming with Basar
Views: 21,953
Rating: undefined out of 5
Keywords:
Id: cWi7TAyVoZo
Channel Id: undefined
Length: 13min 46sec (826 seconds)
Published: Sun Jul 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.