How to use Logging in SpringBoot Application? | SpringBoot Tutorial Part 6

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to spring tutorial today we will be discussing spring logging spring boot uses commons logging for all internal logging but leaves the underlying log implementation open default configuration are provided for java util logging log4j2 and logback in each case loggers are pre-configured to use console output with optional file output also available by default if you use the starters such as spring boot starter log back is used for logging appropriate log back routing is also included to ensure that independent libraries that use java util logging comments logging log 4j or slf 4j all work correctly so what we are planning to do today today we will try to understand how the log format works in spring boot applications okay we'll try to observe the the console output as i said earlier by default all the logs go on console but optionally you can also configure file we'll try to do that file rotation if file gets too big you like to rotate it otherwise you know you can have a some sort of system problems right your your application might not perform if if the file size is growing bigger and bigger so you should be able to control like you know maybe 10 mb 20 mb 100 mb after that spring will automatically rotate for you okay log levels as you know if you're working in in spring or java world you have various levels of log error you have warning you have a debug trace info etc etc so we'll try to play uh with those properties log groups this is a very very interesting property not many people um you know use this this log groups but it's really really powerful stuff and we'll try to utilize it um custom log configuration by default spring works spring spring is magical right but it also gives you power to customize it we'll try to do some some customization uh maybe some some coloring of various things you know we'll try to change the default format uh maybe you know the file location etc etc so first thing first let's just go back to our spring boot application so i have my application.yaml this is my spring boot application as we have been working through in previous tutorials so let's start our application first and let's just try to understand what all these various uh you know values mean okay so first thing you see over here is date and time it's precise to millisecond okay so you have your year you have month you have date you have your hour minute second and you have your milliseconds all right then you have your log level you error born info debug or trace this is your process id okay then um you have three dashes after that basically so it's simply saying now the main logging starts okay the dynamic logging starts over here you have your thread name okay main over here because i just started my application uh it enclosed in square brackets by default then you have your logo name right this is usually the source class name and often abbreviated because sometimes you know the package names can be really large and uh spring boot by default try to limit to whatever you know the 50 60 characters over here so as you can see it always try to give you the full class name okay but if it it notices like now the the whole size is going beyond 60 70 character whatever that default value is it will try start kind of truncating or abbreviating your package names okay so if you're just seeing for example osbae it's probably like you know coming from uh i don't know maybe org spring framework etc if you'd like to really see it if i just try to see this file say include known yeah org spring uh framework boot activate and point web okay boot activate uh endpoint web okay that's that's that's good stuff and then at the end after this colon you have your actual log message which you print in your application make sense by default again many things are by default when you start your spring boot application it typically prints on you know typically prints errors it prints warns and it prints info if you like to print debug when your application is starting you can go to your application.yaml you can simply say debug equal to true or debug equal to on just remember one thing this debug is for internal spring libraries only such as spring boot libraries hibernate or maybe other internal libraries which spring is using this is not controlling your application's log level there is another property to do that we'll take a look in a second so if i just put debug equal to true just notice over here at the bottom so far we have been seeing info info info because there is no error there is no warn if i put debug equal to true and if i restart my application what you will notice now we have started to get debug level entries as well okay so this is how you control it you can put it into your application dot yaml or you can specify that parameter if you are starting spring boot from command prompt what you can do is you can simply say java minus jar your application jar space dash dash debug okay and and your application will start in debug mode okay let's get rid of that we don't need that typically you don't need it stop our application what's the second thing we want to do let's just try to um go to file output okay as i said previously everything by default goes on the console you can however tell spring that you like to output into a file as well so there are two ways of of doing it one let's just put something over here you can say logging yeah logging dot file name and path there are two things let's start with path first okay if you just give logging file path what spring will do it will go to that path and it will create a default spring log file okay you can however give your own file name as well which will take a look in a second so path let's just simply say tmp and we can go to a folder called sagu okay and that's that's about it so if i open a console over here c d t sorry somebody in my previous video said they don't like green color so i'm gonna open something in in black over here okay so it's much uh more visible and let's just try to increase the font as well okay so let's go to cd tmp sago folder over here okay and the directory does not exist what spring will do it will automatically try to create the directory for us hopefully it will do so let's just say logging file path tmp sagu and let's start our application okay our application started successfully which means it didn't complain it has right to create this directory so if i now try to do cd tmp sagu hopefully perfect i'm able to go in there if i say ls minus l there is my file spring dot log it did not exist before it just created at 7 14 okay that's the time 7 14. so what do we have in the uh spring dot log so let's just say get spring dot log and whatever you see in in your console at the bottom as you can see in the in the in the background same thing will be now printed or written into this file really good stuff you didn't have to do anything you just had to specify the file so as you noticed by default what spring did it created a file called spring.log now i don't like this this name okay let's say i have running quite a few application i like to give a slightly different our tutorial probably is uh what is it like i think we were building some sort of eshop so what i can do is i can say spring dot file dot name okay and name basically takes your uh path as well as actually you can specify path and then the file name let's try both things so if i just simply say eshop.log if i do that in one line i can i can specify the path as well as my file name okay now if i restart my application spring will start to write into this this file good stuff let's just come back run ls minus once again let's just put cls before it okay so as you can see over here now we got eshop dot log file created for us good stuff okay um you you can play with like you know having path separately and the file name i hope it works if it doesn't work name name is there there for you now what was the next item in the in the list file rotation okay as i said by default spring um if you by by the way if you configure this option ie you like to have a logging file um by default whenever the file size is 10 mb spring will rotate for you and it's gonna keep for seven days or something like that i think there are some some default values you can you can read on the spring dock if however you like to either you know reduce the size or you know increase the size you can do that how to do it you simply go back to your application dot yaml so let's just leave this bit over here you say logging log back uh max file size you know the beauty of intellij is it can actually show you quite a few things as you can see over here it's saying by default max file size is 10 megabytes let's just click on that and what we can do for our testing purposes i can say 5 kb because i'm not not writing a lot of stuff so hopefully um that that should do the job for me also what i can do is i can say debug equal to true reason why i'm doing that so i can i can literally create a lot of content to uh to start with come back over here let's just empty everything in this folder remove asterisk okay so we don't have anything at the moment yeah my my directory is empty now if i restart my application let's see what happens okay my application started successfully if i go back to my console if i say ls minus l you can see it's eshop dot log and it should have rotated but why is it not doing it maybe still probably it's not 5 kb so what we can do is let's just try to put some some additional content let's just say for integer i equals 0 i less than 100 i plus plus s out to start with s out for now okay this is just a message and let's just print a message number okay so i hope this should be enough to have multiple files if i come back over here oh perfect yeah so i think the the previous um content or the data which we are putting on our console was not enough to to reach that threshold so what happened over here as soon as spring notice that the file size has increased increased the threshold which we have specified over here it archived the previous log file and have started the new one good stuff a few more things are available and i'm just going to show you but we don't need to go to each and every option if i just do control and space you can see you have a you know rolling policy maximum file size which we tried you can have a file name pattern as well so by default we we saw it has a file name it has like you know the timestamp and it has the the number you know sequentially it's gonna increase and then it's zipping it if you want you can you can change any of the things over here which are appearing another thing you have the third option clean history on start okay so what it simply means is let's say your application is running for i don't know maybe eight or ten days and then it created a lot of archive stuff if you want spring to you know clean everything at startup you can you can uh turn this flag on this is a boolean as you can see in on the content maximum history by default i think it keeps um seven days oh yeah but not yet you can see over here if you like to keep it for longer period 10 days 15 days whatever your data retention policy is you can simply configure that that number over here one day 10 days 20 days who knows right um total size cap total size of log backups to be kept okay very simple stuff so you can play with all these things i'm just trying to demonstrate that what what various options are uh available let's just move on so we can undo this bit um i.e let's just delete it we we don't need it we don't also need the logging file name okay so we are we are back to normal what was the next thing we wanted to do next thing we want to do is log levels yeah all the uh supported logging systems can have the logger level set in in the in the spring environment okay which is our application yaml by using the logging level okay uh let's just just try to do that so in the logging you have let's say logging then you can say level and what you can do is you can say root is probably error what does it what does that mean so when spring boot started okay that's basically called root you're saying when spring application starts how do you want it to print the the the logs okay by default it's printing one info and error if you don't like info and you know all these things which were printed in the beginning if i if i show you okay if i just go back at the top if you don't even want like you know this info to be printed because sometime it can be you know very noisy stuff you can tell spring to only print if there are errors at startup make sense let's start our application but you will notice it this time our application has started very very silently okay forget about this this is just a message because um we are printing manually over here system.out.println it doesn't have a logging level okay so it's just printing on the console so what you notice over here our spring boost application started very quietly it did not print info worn etc and this is how you how you control it another thing you can do is now we are going to the main bits okay if you like to control logging level of your application or even at the class level what you can do is you should not be using says out by the way okay you have to use logger so if i come back to my products application this is how i have um kind of created a instance of a logger interface over here you can see loggerfactory.getlogger you pass the class name now you get the flexibility to tell spring or your logger at what level you'd like to print your messages so i'm simply going to say log dot let's start with info okay and i hope you know that you can pass as a parameter rather than concatenating the string because that's much more performant much better because things will be evaluated like no later on not not right away um if i do that and if i start my application okay let's see what happens nothing printed why nothing printed because we told spring to print only if you see error messages now you must be wondering if i change info over here everything will just go on on info right i don't want to do that what i do not want i want spring or startup to only print at error level but i would like to have a fine grain control for my class what my class is products application and i want spring to print at info level for my class how do you do that you come back over here so root is root controlling the whole thing over here we need to give the package name plus the class name okay so i'm simply going to copy my package name over here now two things you have to remember you can give at package level and it could be as deep as like no maybe very last package or it can be parent level or you can actually go to a very class level as well okay so if you want you can actually give the class name over here too what we are going to simply do is we are going to we are saying all the classes inside eshop package should be printing log at info level see the console at the bottom earlier even though i asked it to print at log dot info level but it did not print now we have configured our package to print at this level if i restart my application there you are can you see now my spring did not print anything okay it still silently started but my application now com sago eshop products application is printing stuff at information level this is just a message zero one two three four five that's exactly we wrote plus other stuff is printed because the spring boot application so the things which are inside over here they are printing some some information that okay application started blah blah blah all these things this is how you take control of your application package level or at at very class specific level okay sometimes you have a need like no maybe one class you would like to run on a trace level or debug level but other classes you just want to keep it on on error or warren level does it make sense okay i hope it does now one thing if you if you remember from my presentation i was saying about the log groups what is log groups log groups it is very useful feature to group things or packages and give them one name otherwise what's going to happen is let's say i have i don't know 40 or 50 packages right i have to go individually you know specify that okay for this package you give this information for this package you know print at this level what if i like know maybe like to group things what i can do is i can create groups such as database you right so anything which is like you know maybe oracle database or whatever database i'm using so i can say you know take all these packages oracle plus my own dows etc etc give them a tag okay and later on i would like to control those things using a tag you can put a few things under spring you can put few things under web you can put you know your own stuff under under certain tags how do you do that let's let's try it out then it'll hopefully it'll make much more sense so on a logging level you come over here you start to specify group okay you say group and it's saying happen that's quite funny okay so you can uh give a group let's let's call a group uh maybe what shall we call let's call it spring yeah let's call it spring what you can do is in in that case you can say you know what use anytime you see or org spring framework or you see org.spring dock is very clever right if we did not have this mechanism what you you'll have to do you have to individually you know specify the the logging level for these things but now i can put as many folders i or packages i want over here classes packages etc etc it can be 1 to 20 hundred it doesn't doesn't really matter there's no no limit as such also what you can do is you can put things over here you can say eshop dao you want dow to be printing maybe at warren level oh sorry we are not specifying the level uh we are simply specifying uh what am i talking com dot sagu and i can say look look over here eshop dot dao yeah i'm saying anything under dao i just assume like you know inside dao if i had multiple packages i'm saying anything which you see under tao group under eshop dao but anything which you see under i don't know maybe controller okay eshop controller at some certain certain level and eshop itself uh that might cause issues so let's let's keep it like that that okay we are creating eshop dao eshop controller etc etc what you can do over here then is the good or bad thing with yaml is that it kind of you know creates everything on new line what what i can do is i can remove these these things and i should be able to put with dot and hopefully it's just gonna work um instead of me now giving the package names etc what i can simply do is i can actually let's try to create one for just eshop i hope it works okay my only worry is that it doesn't get confused with with the top levels okay so what i'm simply saying is over here eshop dash dao if i see dao print at war level however if i see eshop controller maybe put controller at info level because we like to see and i like to print eshop just eshop at info level as well i hope that that works okay let's restart our application just to try it out first uh okay it worked that's beautiful as you saw nowhere i'm specifying the package names at the level but i simply gave under group and it worked okay just to prove uh if we go to our dao and in dao let's say whenever somebody is requesting a product okay one more thing while i'm here you can you can define logger like this as you see logger factory blah blah blah all these things or what you can do is if you have lombok as a dependency you can simply put sl4 j annotation at the top and then by default you're gonna have a log okay so what i'm going to do is in the constructor of product tao i'm simply saying log dot uh what did we say at application dot for dao i'm saying war won't yeah so let's say i'm printing info and i'm saying hello entering this is not actually info this is just a you know debug because sometimes you just like to put some some random stuff just just for debugging hello uh constructor construct replace with constructor wow sometime but what we said we would like to print only it at one level so just to prove that it's only printing at one level this is a warning that i have added three products only i don't know doesn't make sense but who cares okay so if i please warning yes come on guys i i know spreading a little bit here if i restart my application okay just notice one thing okay you know what this this damn thing is is causing a lot of noise so let's just print only two logs over here and restart our application because i just noticed quickly it did work but i wanted to avoid the noise okay so notice one thing over here we got info from product application but as far as product dao is concerned we got only at warning level that's what we specified i said for dao just give me warning even though in my tao i had a debug but this did not get print if i want i can change that to debug restart my application and you will notice that log entry will be will be coming can you see debug one is there all right this is this is really powerful stuff let's try to move on so before we finish i think the last thing which i wanted to show you was so this is all the default stuff okay what you can do is you can drop a log back file log back xml file which gives you you know much more you know fine grain control most of the things you can do over here in the application.yaml file but sometime if you don't want to pollute your application.yaml file you can also use a file called log back spring and what i have done i have renamed to dot txt now i'm gonna rename it back to xml so if you drop your xml file log back xml file over here in the resources folder this is where it looks basically you should be able to specify everything over here one thing if you like you can change the the pattern how you printing the message you can you can change various colors you know i i try to play with yellow uh some somewhere i put probably red or or blue etc you can you can specify your log files you know you can specify um you know how you like to control for your your package etc etc so if you don't want to control things in application.yaml you can you can control using logback spring xml file as well so if i restart my application now this file will will take the presence ah as you can see already okay one thing you will notice already now one is printing in red error is also printing in red earlier it was not red plus the the format looks slightly different my message is now in square brackets because this is what i try to do over here just to prove message is going like that yeah so i hope um you you understood like know how um log logging works in in spring oh my camera has gone very dark i just didn't notice that light is not on in this room uh any hope um i hope you like this video and and just play okay i'm gonna check in this this project on on sagu dot uk so you can you can check out you can play and logging is something like now you have to play this is how you're gonna learn it okay i hope you enjoyed this session um please subscribe if you have not give me thumbs up and leave leave comment at the bottom okay i'll see you in the next video thank you
Info
Channel: Saggu
Views: 102
Rating: undefined out of 5
Keywords: Java, Spring, Springboot, Tutorial, Code, Logging, LogBack, Log4J, SLF4J
Id: qBG5VrSPGyo
Channel Id: undefined
Length: 31min 43sec (1903 seconds)
Published: Fri Sep 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.