Production grade logger in Javascript | Winston

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey there everyone hitesh here and welcome to another late night show and in this video i'm gonna walk you through with the production level logger why do you need it how you can use it and a little bit more on that before we go ahead and talk about these production grade loggers do you know that there are applications or tools like data dog logly by the famous solarwinds or i'm pretty sure you've heard about the splunk these are the tools which are designed to process your logs and give you a whole lot of meaningful information but the bigger question that comes up here that can these log actually just work just based on the console.log of course not we need to feed them up with proper more log information so that we can get some more meaningful data but what kind of meaningful data okay let me explain you that do you really need such kind of loggers or not because bringing up a bazooka in the knife fight is not really the way we do it so let's just say you have a website which is your personal portfolio or a personal website probably 30 to 40 or maybe 50 people's people in a month watch this website or come to visit the website to know more about you putting up a logger in such a website is really an overkill you should never do that even think to do that but on the other side there is an application which is being used by two hundred thousand three hundred thousand or four hundred thousand people and you want to know more about the people who are hitting what routes and probably you want to know that how many users from the single login are using your app how many ips are going on into so that you can find out how many people are using the same account so that you can go ahead and block their account yes that's all possible that's all is being done by using a professional logger so both applications are hosted on probably aws or maybe big giant you can call them that both are in production but depends on the scale that you want to use these kinds of professional loggers or not so what we're going to do in this video in this video i will first walk you through one such similar kind of logger which is winston but yes there are others as well i will walk you through that how you can use winston how you can configure that first we're gonna configure that as a fun youtube level logger and then we're going to move into a little bit more onto production grade logger then i'm going to walk you through that how you can utilize and read a little bit more documentation to put these logs into a file or maybe later on you want to put it into a database i'll walk you through there as well we'll guide you towards the documentation on that part now while creating this video i have especially kept in mind that some of you might be coming up from react vue angular or maybe other kind of frameworks or libraries node.js express fastify probably god knows what you might be using so i want to keep this video absolutely independent of any kind of library or framework we're going to go through with the plain classic javascript now one more thing just remember that you usually don't configure these loggers if you're just getting started into the company usually the senior members of the company are responsible for getting these loggers configured properly and what kind of information they want to store in the log but if you are a guy who is doing a startup or getting started with the product or you are the first one in the company then this is a great video for you and you'll be able to configure and put all the things into professional logs and yeah by the way this is the same icd but it's so good to drink in this one anyways now let's go ahead and move on to my computer screen and let me walk you through with this winston okay so let's go ahead and talk about winston and configure that but before that here are some of the examples of i was talking about the in the softwares or the tools that helps you to analyze logs in much more detail one of the famous one is datadog which is really popular getting more popularity the one is loggly which is by solarwinds and if in case you don't know solarwinds is like the most go to log software for the big giants or uh top 500 companies in usa but this is really popular in the united states and another one is splunk which is new really popular and especially in india it's getting a lot of popularity anyways moving on to the winston so this is the winston library we are going to configure that install that we'll read the documentation together so let me fire up my vs code up here and in the vs code i'm going to go ahead and drag and drop a folder youtube winston which is an empty folder okay let's go ahead and move it all the way up here so that we get more real estate of the screen okay the first thing we're going to go ahead and do it is simply saying npm init dash y so that it doesn't keep on asking me the questions and in here we can see that we need to have an index.js file which is kind of a standard let's go ahead and say index.js okay this new file is up here we need to modify this as well so instead of the test we're going to say this is a start script and this script is going to be handled by node index.js so whenever i say npm start this command will run node and index.js in the index.js we have seen that we have this console.log which is quite quite a regular one so we'll say log information i can just go ahead and copy this and paste this one more time or two more time so we have seen that we have some of the warning logs some of the info logs and what else we have debug logs as well error logs and a whole bunch of other things and this is the basic stuff and i'm going to go ahead and say that whenever i say npm npm start this is basic information we get log information this is probably warning information this is info info information okay let's get with that and this is a debug information let's run it one more time so that we see that yeah these are basics and in case you are using something else or something other terminal probably you'll get a little bit more color coding on that but we can define that no big issue i'm going to go ahead and remove this log because this is where we are most interested in that okay so this is all basics now let's go ahead and move on to the winston to understand that how this is being done and what we can do so if i go back up here into the first usage notice here this is divided into actually two parts the first is this part where it says that how you can create a logger and put more configured information on that and the second is how you are going to configure your environment variable now this totally depends on you that what are the parts that you want to change in the logger based on whether it is in production is it in development or maybe just on youtube so we're going to take care of that little bit later this part we are not going to be copy pasting from the docs because we every application has its own way of managing the environment variables some uses dot env file some goes like this and there are other ways as well so feel free to use whatever you're using coming on to this part where we are configuring our logger the most important thing here is the level the format and the transport the level is something which everybody should know so let me walk you through with this so go up here into the logging levels and here we can see that there are two types of leg levels the syslog which we are not using so we are not going to study that and another one is npm which we obviously are going to study so this is one here so every level has a every log has a level the first one we can see that error has a level of zero and all of that the most important thing is if your level is set to info that means your level of the log is set to two info all of the above logs are allowed to be printed but anything below that so http log cannot be printed if your log level is info if your log level is debug then all the logs which are above are allowed to be printed but the silly is not going to be printed that's the one thing that you should really know so let me go ahead and move at the top again so this is the first thing then the second thing is the format usually all of the three giants that i've showed you which can process the log they require these logs to be printed up in the format of json that's kind of a standard industry standard and the third one is the transport this is where you define that whether your log should come up into a file uh maybe into database or just add the console this is where you define it okay you got this one i guess that's all what we need for the basics now let's configure a little bit more on that so first let's go to the package.json we won't be installing the dot env we'll be just throwing up the environment variable right from here so here you can actually go ahead and say node underscore env and can say that not equals you can say youtube you don't put equal this is not a string this is an environment variable so this is how you do it in case you don't want to install env file but this is very basics okay that part is all done now let's go ahead and first install the winston so we're going to say npm install winston come on there we go looks okay now winston is a very very small package and it's going to be installed before i say it that it will be installed okay that's part is good now how do we do it whether you're using react angular view or maybe anything else this is how exactly everybody does it so you'll you'll see exactly same thing happening in your company as well there is usually a new folder called as logger and inside this you get an index.js file okay so why we are having this one to actually control more about how things are happening so let's just say here we are having a logger and that starts with null okay and then you define this logger based on on which environment you are so that if anybody is here in the index and using console.log in any of their application they can use it directly by saying i don't want to use console i don't i want to use logger that's our company policy we don't use console.log we use logger dot whatever the name is there and all you have to do is is simply say that i want to have this logger and that logger will come up from require and let's just say the logger that's where it is coming in okay this is resolved right now i understand this part but the problem is that how this logger will take on more information let me show you that from the documentation let's go ahead and copy this entire line and study that in the docs up here okay the first thing i want to do is obviously say module dot exports that is logger so now we are throwing this logger back and this guy who is saying that i am importing that he is able to import that that's nice and easy okay now the important part that comes up is that how this logger object will be changed or how we are going to configure this right now you can see that the format is simple and there are some objects are configured properly here but right now instead of saying that logger is going to get an ad and all of this let's go ahead and define this logger in its own separate file in a minute and i'll say that hey logger you'll be getting all information from something known as youtube logger method now obviously the question is where is this youtube logger method it doesn't exist we need to create that so let's save this one up here and i'm going to go inside this logger and i'm going to say all the things that i want to do as a log on the youtube i'll just store that into this youtube logger.js file okay nice and easy so the first thing that i'm going to do here here is i'm going to say this is youtube logger this is a simple method you can use the regular javascript method too that's fine and i'm going to go ahead and say module dot exports that is youtube logger okay that is nice and easy there we go now another thing we obviously have to do is go into this logger and make sure this youtube logger method is being imported because we are doing a module.export so let's go ahead and say that i have a youtube logger which is coming up from require and we are saying please bring this up from youtube logger so instead of just configuring this logger right here and meshing all the code inside up here we're trying to keep everything into the separate file that's kind of a standard practice in the production this is all great but one thing is still bothering to all of us is what's going on inside this method i have no idea how this will be configured and that's what we are going to study in this winston the first thing obviously is to bring up this very first line so that we can have a usage of the winston okay there we go that was easy now let's go ahead and study this so how we're going to study this we are going to copy this entirely and we'll just move up here and we'll say hey let's just paste this up here okay that is fine that is fine so far now let's go ahead and make it runnable because this is going to give us an error because here we are defining another logger and configuring it instead of that now everything is handled by this youtube logger a simple javascript so we need to return this entire thing so instead of just storing that into a variable logger let's go ahead and return this directly okay that makes sense now we are saying that the level is info so if my file is having something like debug it's not going to be printed that is obvious the format we are using is json the default meta we don't need this information much let's comment this out and here in the transport we are saying we are storing everything into the error log and we are storing that another one more time into a file name of combined log okay makes sense but let's see if we are able to actually run this directly or do we face any problem or issues that is how we learn let us go ahead and say this one and i am going to say npm start let us go ahead and run this ok something happened so we see this combined dot log which is having some of the information notice here the warn and the info only nothing about debug and there is an error log as well which is empty okay that's nice the reason for that is because we are having some of the levels being defined and all of that okay that is nice now it's time that we actually go ahead and play a little bit more with that okay first i'm going to go ahead and change the level to debug and obviously i don't want to just create so many of the files i'm going to go ahead and change all of this so instead of saying that all of this information going like that let's remove everything up here and as you can see this transport has so many of the options you can just throw that up into the file you can say http to so that you can stream the data or you can use http and there is a separate for streaming maybe you are constantly analyzing in the real time maybe you are using some of the aws services yeah you can do that and there is a format there is a json there there is a lot that you can go ahead and use up here i'm going to go ahead and say this this is a console so whatever you see just don't throw that into a file just throw that onto a console screen so that i can see it right now and i'm gonna go ahead and remove these comments unnecessary so there we go okay nice and easy so far let's go ahead and run this one more time so now we can see that all these messages are being printed up here okay that is nice for now i know how to store them into file i know also how to store them or just display them onto this screen but now let's go ahead and work a little bit more let me show you more on to the documentation if you go up and read this entire table of content you're going to see so much is there one thing that is going to obviously catch your eye is the format so there are a lot of formats being available and as you can see that instead of just having this logger just like basically we have we can actually use this format and then we can use this combined method to give it more information than it is having so let's go ahead and do that we're going to go ahead and copy this entirely and paste it up here so now instead of having this winston.format we are going to go ahead and say uh let's is it winston.format because this is actually now copying all the things up here yeah i will do that okay so let's go ahead and paste this up here okay this is good so far and we don't have this my format method being defined so let's go ahead and define that also so we'll bring this method up here and copy that so this is how you can actually define your own custom format the way you wanted to feed it to other data dogs to analyze and further stuff we're going to go ahead and remove timestamp is good label is good message is also good we are not playing around with the levels labels so let's go ahead and remove that and this is good and my format is all good we don't need this label of meow that's why we are not playing with this one okay that is nice as of now i guess but this is going to give us some of the issues i am pretty sure of it okay let's go ahead because this combine needs to come up from because notice here in the documentation they are saying that i am actually destructuring this combine from the format which is coming up from format and this is coming up from winston so we need to say winston.format dot combine and that is too much let's go ahead and do that not a big issue we can restructure that that would be better but winston dot format dot combine that's actually too much yeah let's do this we can restructure it or let's see how many errors we are going to be facing okay so this is going a little bit that printf is not defined okay i get the idea printf let's see how or from where this is coming up so printf is also coming up from winston.format.printf come on okay so this is here so winston dot format nobody uses it like that everybody destructure that but let's go ahead and see that having some fun with the winston okay let's go ahead and say that npm start there is again something missing up combined okay i'm gonna go ahead and destructure it because this is getting too much i'm gonna go ahead and copy this and i'm going to go ahead and comment this out and we'll paste it up here there we go and now i don't need to say all of this because otherwise it will drive me insane that this is so much going on we even don't need to say winston dot because we have destructured it we do not need to say it like this because we have destructured it and transports yes we have transport as well so we don't need to say winston.transport we can just say transports yeah thus this makes sense now okay hopefully let's go ahead and clean up the screen and say that okay now we are seeing more options and information going on here so what i'm going to do is just where the timestamp is this is like the default server time i'm going to inject an object and i'm going to say that i need a different format so i'll just say format there we go and the format i'm interested in just hours minutes and second classic javascript let's see if this makes sense a little bit more now uh yep this is a little bit more sensible now oh actually there is a small error i actually accidentally removed this level i should have not done that okay i hope you are enjoying this i'm gonna go ahead and copy this and we'll paste that okay now this should be a little bit better let's go ahead and run this one more time and there we go finally it's better so we are having first the time then we are having warning so this is my custom format that i have defined up here first is timestamp then there is a level there is a message so in case you want the levels to come up first you can go ahead and do that that's basic classic c c style of time stamp that we are going on for that so this is all what we are doing up here with the some of the fun stuff now let's go ahead and work a little bit more now some of you might be really interested in the part is let me go ahead and move in the documentation again okay so here you're going to see that there is an option of colorizing as well which attracts a lot of people i'm not able to find it so let's go ahead and say there are option of colors so there we go you can define all the colors however you want for the levels of these colors so just like that and all you have to do after that is just say that hey i want to use this colorize and there we go winston.format.colorize so yeah that's easy we can come up here and say level just after the level we can just not here in the format we can just go ahead and inject it here i guess color rise and there we go put up a comma hopefully that is it although it says there is a winston.format.colorize i don't think so we need it let's copy this just for the sake that i haven't done any mistake save that and let's see how it works actually this time or maybe we need to debug it more and there we go it is having a colorize so let me go ahead and add it a format i guess that's how this is being done let's go ahead and see that and there we go now we see more colors into the warning and info and the debug okay again this is how everybody learns about a new library or winston there are other famous one in the production as well but this is how everybody does so notice here it says winston.format.combine and here it says winston.format.colorize and then you can use the format of json or anything usually json is the standard format you can change the background colors and foreground colors you can inject these so there's there's a lot that you can handle up here okay so this is all the playing around with the libraries that we are having okay but now it's time that we go ahead and see that how this can be used into multiple environments and multiple production goes when it goes on so notice here we say is index.js so now we can go ahead and literally just copy and paste this one and we can see that this is all you're playing playing stuff which is not equals to production but rather we can actually go ahead and say that go ahead and use all of this when your environment is youtube go ahead and use all the settings when you are having this production this is fine and we can go ahead and change this one to production production logger so from where this production logger is going to be coming up of course from a file which we don't have production logger there we go and this is going to be coming up from require just like this and we're going to say dot slash the file doesn't exist but we're going to call this one as production logger yeah that's fine let's go ahead and copy this and say oops and save this one and let's have one more file up here which we'll be calling as product come on as production logger.js so we have a new file all we have to do is just go ahead copy all of this data because we have played around and now this is a production and instead of saying like this let's go ahead and use the production logger yeah we are exporting a method known as production logger which will be exported just like that so that is absolutely fine and probably in this one we don't want to play that much we know that this is exactly the format or probably we want level to be coming up first so let's go ahead and maybe this is the exact format that your company is following up then we need a timestamp and all of that we obviously are now in the production mode so we don't need a timestamp like this we need an exact server timestamp so that is fine obviously in the production this is never ever recommended that you go ahead and use the colors and the level uh depends maybe it is in the production so we don't want the debug level maybe we want an info level and obviously we just don't want to have a console log we need to store that into a file or maybe we want to dump that on console.log as well so let's go ahead and move on to this guy and there we go and we want to have this one and we'll be saying new not mu new and we'll be saying transport dot and you can see there is a lot of things going on we'll be going for just the file just like that and inside that we can pass on the name of the file as an object so we'll be saying file name there we go and we'll be calling this as my errors dot log maybe whatever the file name you're going on and there we go just with a few clicks your production level logger is ready maybe you want to add more you can go ahead and do so and now all you have to do is go up here and change the environment variable that now we are in production and there we go that's how we change all of this now let's go ahead and run up this that how it goes on and look up into the production so we say npm start and no colors there is no debug and this is exactly i want maybe first the log messages then exact server time and then we are having warning information or information like that okay so this is how you go ahead and configure a production level logger of course there were a lot of errors in this video i'm not going to skip them out because it is important for you to understand that how we go ahead and play with the documentation have some errors fix them up this is how everybody plays in real life this is not uh some polished and stuff like that so there we go but one more thing i want to show you here is that this is not the only famous library there are many other libraries as well but what makes you a little bit surprising that this library is so much used and so much configured along with mongodb that if you go ahead and say winston db there is a standalone library here for the npm and all you got to do is just install it and the configuration is like super smooth you just simply say require winston mongodb so yeah we go ahead and require that and then we simply say winston.ad and we just simply say new winston.transports and then you pass on all the options here options which has a mongodb string new uri parser so if you are familiar with the mongodb this shouldn't be much more of a nightmare for you so totally this is like so much of the popular library that people don't just use winston they also like to store logs into mongodb and maybe lots of other places as well so now that you know and i'm pretty sure you are going to use it much more in the production environment but please don't over kill that if you have enjoyed this video make sure you hit that subscribe button if you have enjoyed it more go ahead share this out with the world otherwise no problem at all we can still be friends and hang out on instagram catch me up there and yes i'm gonna catch you up in the next video [Music] cause i got somewhere to be and you're done deceiving me into thinking i couldn't do better yeah i know i was probably crazy thinking someday you'll change there's nothing left to do now so i'm playing the game and so heartbreaker i've been working you out you made loads of mistakes
Info
Channel: Hitesh Choudhary
Views: 14,843
Rating: undefined out of 5
Keywords: Programming, LearnCodeOnline, python, javascript, winston, logger, datadog, loggly, production logger, winston mongodb, console log, javascript log
Id: m2q1Cevl_qw
Channel Id: undefined
Length: 27min 40sec (1660 seconds)
Published: Wed Jul 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.