Logging and log4j.properties explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to talk about logging log for J and how to set up log for J with Maven in an eclipse Dynamic web project logging is very important to consider throughout the entire development life cycle we only realize how important is it is once our application is rolled out and in production and especially when we start getting errors with it when we start getting errors we have to play CSI sometimes and we have to go back and investigate try to replay what happened the only way we can do that is if we have enough information to tell us what happened and logging is how we get that information now log forj is something that's been fairly standard within Java programming for quite a long time the there aren't honestly to my knowledge there aren't many competitors to it it's just everybody uses log for J it is industry standard free reliable uh very frequently used easy to configure with log for J properties if you know what you're looking for now with log for J properties we're typically going to define a few things first of all not all log messages are created equally there are different sever severities of log levels and we have to ask ourselves the question what do we want too much information in the logs or too little or maybe something in between hopefully something in between so we have different logging levels we're going to take a look at on the next slide an appender is something that uh publishes logging information now we typically think of logs going to something like a file but it could also go to an email or a server somewhere via a message it could go to an error window a console it could go to several places the same log can go to several places so the appender is what publishes it to these places now we also have a logger a logger is what essentially collects logging information and provides it to an appender where does it collect the logging information well it collects it from the source code and multiple penders can be subscribed to the same log the root logger is considered kind of like the default logger for the entire application now both loggers and appenders can have logging levels set the logging levels look like this at the very top we're going to have fatal that basically means that our application is going to die uh error is next you're going to as you see there will be more of these as we go down in other words we're going to see a whole lot of debugs not so many fatals hopefully so uh fatal uh is is one hopefully we won't see too frequently error that's something we would probably log in a catch block info uh maybe we want to make note of something like well the species is empty are you sure you want the species to be empty worn uh a little more granular than info and then finally debug would be something like we're entering a method we're exiting a method so you see we get more verbose as we go down okay now we're going to configure these things with a log for J properties file which we'll look at here in just a moment uh in our virtual machine I have preop pied a few things we're going to need in the notepad here first of all we're using Maven and in this project and so what we need to do to grab log for J is simply add this dependency here now don't worry about copying that directly from this video uh because I will commit this information to uh GitHub and so you can just grab it from there but I'm going to go to the bottom of my um pal which is now getting longer and longer I'm going to put in this dependency control shift F and uh we will let it realign just a moment there there we go and save okay next thing is we need a log for J file a log for J properties file we can either do XML that's actually the preferred method or we can do a traditional properties uh name uh name equals value file like we see here I'm going to do the properties file honestly for a strange reason because the XML file is pretty easy to read and you can pick that one up fairly quickly the properties file which I see just about as many properties files as I do XML files the property file is a bit harder to understand and so I want to show it here just so I can explain it so that way you can choose whichever you want when you implement this either the properties file or the um XML file so first of all I'm going to copy this information and right click on source and I'm going to say new file and just other file will be fine and then we're just going to say General Old file and then watch my capitalization here log for j. properties so should be spelled just like this because this is a magic file that it's looking for and now I'm going to paste my default information now one thing we've put this in our source tree that's kind of considered a little bit dirty to put a properties file there it's okay it will work it's just kind of considered dirty incidentally this is the default place where it's looking for a log for J file if you don't like putting it there that's okay what you can do is you can use this flag that I've highlighted here which is- D log for J.C configuration equals and then after the equal sign put a path to where the log for J properties is if you're running this in a web application particularly within Eclipse double click the go to the j2ee view servers tab Tom CAD V8 open launch configuration arguments and then this is where you put any jvm arguments notice they all start with Dash g- D rather uh just like the one that I showed you that starts with a-h d so that's where we would put that but anyway we're going to go ahead and leave it at the default we're going to leave it right here okay deep breath what are we looking at here okay the logger remember this is the one that's Gathering the logging ination from our running application and this is the root logger the default logger what's confusing here is we have a comma separated list and the items on this list are not the same the first item is that logging level remember what our logging levels are from the presentation fatal error info Warn and debug so what this means is we're going to produce errors only that are info level or higher and we are going to uh publish them where that's where this list gets a little bit confusing after the first element we have a comma and then we have another element called file this is a list of appenders and I can have more than one appender so I might say file and then I might say priority file or something like that or something like that each thing that follows after this logging level is the appender or are the appenders that are subscribed to this logger do that make sense yet don't worry if it doesn't I realize it's a bit confusing how do we know what this file appender is well we have to look for a very specific syntax in log for J and appender starts with log for j. appender and then finally the appender name so file here matches to file here here here here here here what we're saying is that in this area we're defining an appender and its name is file we're subscribing this appender to this root logger in other words root logger gathers logging information gives it to the appender the appender prints that logging information in this case to a file and my apologies if I was a bit redundant in that explanation but it took me a good long time to understand this myself the first time I looked at it it was one of those things where I always knew which property to change but I never understood that line number two and that that line never made sense to me okay until I went through that explanation so this file appender what we're saying is what type is it well it's a rolling file appender it's going to send logging information to this file once that file gets 10 megabytes in size it's going to roll it over what that means is it's going to back it up and create a new file we're going going to allow 10 backups after the 10th it's going to start deleting files that are old okay so it will back up the current file to like a number one back up number one to number two number two to number three all the way up until we get to number nine nine backs up to 10 the old 10 becomes deleted so the oldest backup file always gets deleted okay and now what's the pattern going to look like how's it going to look when we print information to this log so that's the information we need to set up a logger and an appender the next thing we need to do is implement this in our code so I'm going to navigate to our UI layer and let's take a look at some things that we can do I'm going to go to add plant okay and contrl M so we can see this in high definition okay uh one thing I need to do is I need to say final static logger logger equals logger and just a moment get logger there we go okay and then the name of the current class which is ADD plant. class okay uh control shift o make sure we have Imports organized looks like we're good there okay now when we log we're going to log to this logger right here remember the logger logger on appender logger is the thing that is assembling or gathering log messages from our code okay so what I might do when I go into this execute method is I might say logger whoops spelled correctly and then we might say info okay and message and we'll simply say entering the execute method so you see that's something that's going to go into the log very frequently because it's information it's info level it's not fatal or anything it's just info okay remember if you you've seen a previous video we have a method here that's trying to save a plant if something goes wrong it's going to skip everything until it gets to the catch block so as soon as we get to the line immediately after the method save and as long as it's before that catch block we know that the save was successful so I might say logger doino and then I might say save successful and then I might even go as far to say uh plus plant. two string now uh careful about this if you're dealing with financial information if you're dealing with financial transactions things like that don't put sensitive information uh into a log file credit card number or passwords anything like that so if you are doing one of these where you're logging some information that was saved make sure it's not sensitive information okay so now we have to it info level exceptions make sure you're putting logs in exceptions that's a major code review item make sure that you're doing it before you submit your project for code review uh if you're code reviewing make sure that exceptions have log messages so I'm going to say logger dot uh this time we're going to say error okay and we'll say error while saving plant message okay and then we'll say plus E.G get message okay this is crucial uh all of your exceptions should have these because when you're trying to debug something later in life uh once your once your software is rolled out you live and die by these messages let's go ahead and jump to the save method here um nothing major in the save method I think this is just a pass through we could do another log info here uh or we could run on down to the insert method we could do another log in this Dao uh you know probably a good idea to spread this logging information out throughout our application as a matter of fact since this is a stub it's a good idea to put a log message to just say hey the stub is wired up uh that way somebody realizes they're not actually saving anything it's just going to the stub uh I've honestly known that to happen before where someone was having a problem in production and what happened is that a stub was put in place not the actual production class it took a long time to get to the bottom of that so we'll say static final logger this time we'll just call it log equals logger doget logger okay and then class here is going to be plant Dao stub. class okay uh lowercase the L and terminate with the semicolon and save okay now we'll simply log down here in the insert we'll say log that we could do an info or maybe a Warren here honestly I kind of think Warren is more let's see if we can do let's do a Warren here what the heck okay log. waren and we're going to say inserting to stub semicolon whoops this does not persist the item like so and save okay now we're in good shape so I'm simply going to rebuild deploy and then we're going to take a look at our logs okay I have deployed uh let's go ahead I'm going to say genus Fu and submit this one should execute successfully I forgot I have the break point debugger on so I'm going to turn off break points and resume and sure enough we see our growl is popping up we allow that to go away and the growl I did in a previous video okay now let me take away genus and we know this is going to cause an error I take away genus I hit submit we get our error growl okay and let's go look for the error file so remember we were we configured this in log for j. properties and it's storing the log in C log loging log okay let's take a look so C log logging and uh September 21st 2015 at uh 10:31 p.m. that's right about the current time and so I'm going to go in here and you'll see that log for J is a standard thing and so we have uh logging information from other things that our application is using if I scroll down a bit we're going to see error error while saving plant message genus required okay uh then we see info the remember I I tried it a couple different times so we see an info here we see the class name that's doing the logging remember when we set up our logger we gave it the class name the line number where this log happen we have entering the execute method okay now this is the time that I actually put in Fu inserting into the stub this does not persist the item notice that the loging level there is warn okay then we have save successful okay now we have entering the execute method which is our info and then error will Saving PL and by the way this error up here I paused the video and I just uh trial what I was about to demonstrate make sure it went okay so that one you didn't see in the video uh the ones that you would have seen in the video is this block this last five right here so the first one was successful we see it warns us about the stub and then it says save successful the next one is where I deleted some information and uh made an invalid save so it tells us we're entering the execute method okay remember what that is if I go back to add plant just a moment okay remember that's this very first line here log info entering the execute method and then after that uh we got an exception or an error it said error while saving plant so let's go back and take a look aor saving plant message genus required so see some very helpful information it's going to be very good for us uh when we are actually rolling our application out now we also see a fair bit of clutter uh because we see that there are things like um you know other messages here there's a lot of info what I typically recommend if you have an error go to the log file go to the bottom search for error Direct up you want to go to the bottom because the most recent logging information will be at the bottom okay and you see that allows you to select only the messages that were logged at error logging is very important the log level is important don't log everything at info don't log everything at error make sure things that are legitimately errors are legitimately errors and things that are info are info okay now remember we can subscribe multiple appenders to one logger and that can help us to kind of reduce the amount of clutter that we have so let's try that out what I'm going to do is I'm going to make a new appender and to make a new appender all I have to do is duplicate this information for my existing appender and then simply change the name uh so let's say uh logger appender let's just call this one error file because maybe we'll only show errors in it so I'm going to replace each thing here with error file okay you see that just by changing that third argument I'm creating and defining the properties for a new appender okay and now what we're going to do is we're going to say we're going to add a new argument called threshold and we're going to say equals error now one other thing let's not forget to change the file where this is going to send its log so I'm going to change the file to error logging dolog so you see I have two different files one that's only showing errors and one that's showing everything that the logger the uh logger is is giving us so one is more restrictive than the other and here again rolling file appender isn't the only option uh we certainly could send a text message or send an email there are lots of other things that we can do with logging to escalate these logs so I'm going to uh Simply Save and restart the the server and after that we'll try it out and we'll see what it looks like just a moment okay and the application has redeployed so I'm going to go to the add a plant screen and we'll do one just normal plant and then one that we're going to make uh with an error so we'll go ahead of course this is actually just going to the stub it's not actually persisting but here's our success we see plant saved we'll give that growl a moment to dismiss and then we will try again with an empty genus which should give us an error and sure enough the growl confirms that we have an error so let's take a look at the error logs notice we have our existing log no surprise here we have info Warn and error a fairly verbose log this includes the log messages that we had before we changed the logging information and restarted and it includes the log information from after uh making that change so you see this spans several restarts of the application server now take a look you see error logging notice it's significantly smaller than logging click into this and we only have the error message so you see that log for J properties we can use that to filter not only the logs that are getting produced but also the level at which our appenders are appending log information to a file to a database uh to an escalation service or whatever it is as a matter of fact if I want to and and errored one more time uh we'll see that we now have two errors okay same information this time a little bit different apart you see the date here the time the logging level the class the line number where it occurred and then the message so logging one of our best friends especially when we've rolled out and we've rolled into support this is something that we do not want to neglect early we want to make sure that we're thorough with our logging in every step of development because when we do go to roll this out those logging messages are going to be very important thank you
Info
Channel: Brandan Jones
Views: 125,879
Rating: undefined out of 5
Keywords: Log4j, log4j.properties, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, Logging exceptions, exception handling
Id: -GkRuFU_sUg
Channel Id: undefined
Length: 22min 4sec (1324 seconds)
Published: Tue Sep 22 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.