How to do logging in java using log4j api, best practices and tips

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello explorers and welcome to another video today we are going to talk about logging in java and what the way we are going to talk about logging we are going to use the log4j framework and we are going to talk about the importance of logging and what levels to use so first off the importance of logging when you have an application you need to know what's happening in the application and you need to have information about both for debugging purposes what is actually triggered in the application and for finding issues and solving problems in your application you can have things that are fatal that it will not happen but if they do it's really bad or you can have something that is just debugging information or trace information that is not really irrelevant but it's interesting for you as a developer to have there so let's jump over to some code and look at some implementation so first off i want to create a logger so we do a final logger logger equals logger get logger and then we have a class name here and i have this blog4j example this class so now i created my logger very simple very easy with this logger i can do log logger and then for instance take info and then log some information and this logger need to be static because i want to use it in this context and for each class that you want to log something you need to set this logger so let's run this and see what's happening and when we run this we it set tells us that we have no appenders that means that we don't have any source where we push these information to so this framework is a way for you in some way to create very succinct messages and create bits of information that has a lot of metadata in it and then send them to a service that will uh send them to your different logging frameworks so let's just create a very simple one we will have the console appender here so let's take a console appender we can call that console appender it's a new console appender so then we have this then we want to take this console appender and set a threshold on it and that's a level that we want to print out so for instance if i say info i then i want to have everything that is either an information or more severe so if we look at the different levels here we can have all then i will get all information into this if you're logging into a file that could be any thing or if you have a logging framework that takes all the information and then gives you what you search for then all could be a good option [Applause] then we have uh trace that's the neven up so you will get everything pretty much everything anyways it's similar to all the bug will not get the trace information just the debug information and then we have info we have warnings we have errors and we have fatals and if we go from the top here if we have a fatal a fatal is something that happens in your application that will make it crash it crashes the application or it's something where you can lose money or if this ever happens it's really bad for the application this is something that we need to solve now not later exactly now because this application isn't in fatal state if we go down one we have errors these are harmful but not faithful fatal so these are things that we need to solve as soon as possible but we don't need to stop everything and um call in the business people and ah and go to work on this 24 7 but it's something that we really need to look into because this is not a good state then we have warnings and this is something that we should look into but it's probably not something that will create some loss in the company but it's something that we need to look at at some point but we might not uh prioritize it for now and then we have the info and that's just information and then how you use debug and trace is up to you these are my definitions for logging and you might have a separate definition of the same terms and that's okay the important part is that your company or the people that works on this application has the same picture of what these words mean because if you have something that is just informational and that is sent to the logging frameworks 200 times every day and somebody has put that as a fatal and you will see it all the time you will get annoyed and important stuff will be missed so it's important that you find a way to create logging messages that is in a level that you are in agreement with the rest of the people in your company so that's the first part that is important about logging another part is when we have these kind of logging messages here let's say that this is an ordering system and here we have just created an order so order created what could be interesting to know if an order is created think about it well you probably have an order [Applause] id here so put that in just say order id like that and we can create a local variable for order id number 42 for instance so what you can add to a logging message in order to make it more uh readable and easier to find that's very good so let's say that we have a try catch here and that something got got wrong it's thrown an exception and we want to log this as let's say a warning it's not super important but it's still something that we want to take care of we can this is the least that you can do you can log the warning at with the message and the exception if you do this you will get a stack trace and know where in the application you are if this for instance up here was order failed and that is something that is should not happen it's very very bad uh we can say that this is an error and you need to find this row fast you can actually add a new exception here let's see if we can if the there if i add an exception to this row so we actually have a message and then an exception i will get the same stack trace as i did before with this row so if we run this example again we will not have any loggers so that's not uh important at the moment but we will get a stack trace and know what did call this action and why what output or where in the stack trace did this happen so we can actually follow the call in let's continue here we we need a few more things to actually get some output we need a layout and i don't know if that is important that you really need and layout but i usually put one because i want the log message to look a specific specific way and you can put whatever you like here i usually create a new pattern layout and then i do date so i want the date that this happened so i will know exactly what second this actually occurred in my application then i want to know the priority and that's the info fatal whatever level it is i want to know the category and the category could be a way for you to categorize your logging if you haven't put a categorization on your logging messages that will be the fully full class name at the moment and then i want the short class name so i just want the last part of the class name so i don't want the package name and then i want the message that we wrote here let's see if we can get another one of those so i want the message that's um dollar sign m and then i want a line break and you could do this just like that but that's not platform agnostic so if you do like this the logging framework will help you and put the right lined endings on in your login log files so if we have set this pattern we also want to activate all the options and then we can take our logger we can get our root logger and append this console appender so now we have added an appender to the root logger and whenever you create a new instance of a logger in any class that root logger will be used and you can put law appenders on different uh levels in your um structure if you like so you don't you need to use the root level but i think the root logger is a really good place to put your appender and here you see that i have in my id here i have my output this order that failed it was order 42 and i also have this exception you would have getting the same information down here but you would get the message of the exception and then the stack trace and here you see that something happened i can create a new breakpoint so it that's a really interesting thing that the id gives you creating a new breakpoint for the exception or you can jump to the row in your source code where this happened and if you have multiple clauses or multiple functions that then you can follow that stack trace down and actually debug and find the issue so that's a really good way to work with this and what is an appender then because there are so many appenders there are file appenders there are appenders for different logging frameworks we have used sentries at work we have used freshlytics or there are so many of them to choose from and most of them have built penders that you can just add to your application configure them somewhat and then send the information over to that framework but if you want to create your own logger or appender it can be interesting to see how they actually work because they are not that complicated so let's create my appender here and that will implement appender and if we do like that and implement all the functions here we can add filters if we like and handle those and we can close it of course we can get the name or set the gate name we can have an error handler we can set the layout and so on or we can say if it requires a layout but the important part here is the do append so let's say that i have a system out print line this event and then just get the message so i just want to print a message and then if we go back here and do again logger get root logger add pender new my appender and we end that and if we run this code with my appender in it we should get the same output as we did before with the console appender but we will get this uh order 5842 here again so that means that we can create your own our own appender and in this do append method we can handle the message or the priority or whatever we want to do and send that to a database or to any other caching or framework that we want to work with so if the framework for logging that you are using doesn't support the log4j framework so you can't ha they don't have any ready appender for you you can create your own the important part is that in your appender you need to if the you have a slow backend create some kind of caching so you don't send a lot of small messages because logging can be very fast and very efficient if you do it right but if you have a lot of messages going through and you send each message over http and you don't can't handle that traffic load then you can have a problem or a performance uh issue with your application so do some caching and then send batches over information is a good approach there so this is what i wanted to cover in this video i hope that you found this interesting i hope that you learned something today if you have any questions or suggestions leave them down in the comment section down below if you like this video give it a like share it with your friends and colleagues if you haven't subscribed yet please do that and i really hope to see you in the next [Music] video [Music] [Music] foreign
Info
Channel: Daniel Persson
Views: 7,920
Rating: undefined out of 5
Keywords: java logging api, FileHandler, ConsoleHandler, Levels, Learn about logging, logging frameworks in Java, What Is Logger, Logging API Framework For Java, Logging information in your application is critical, Logging refers to the recording of activity, Custom File Logging, java tutorial, java tutorial loggin, introduction of log4j in java, log4j in java, जावा लॉगिंग एपी, Java Logging API, Java日志API, log4j file appender example in java
Id: uco-a7c6U8w
Channel Id: undefined
Length: 17min 5sec (1025 seconds)
Published: Sun Sep 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.