How to do logging in Spring Boot - Brain Bytes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so you want to log something in a spring good application how do you do logging in spring boot no not that definitely not that how do you do proper logging in a spring good application and what are your options well let me show you [Music] well I hope that I don't have to tell you why system dot out dot println is a bad idea and we need a proper logging framework a logging framework gives us the benefit of allowing you to configure its behavior rather than just do things with one v for example you can decide where the messages go you can set things like logging levels and you can choose if you want to put it to a file you want to send it as an email you want to save it to database whatever else so yeah for your Springwood applications at least for production applications you need proper logging ok well how do you get started thankfully Springwood is a type of framework that follows the everything included philosophy so it does come with its own logging framework included and as with other things in Springwood it is opinionated it has some default settings and configurations for logging that you can just use out-of-the-box the basic act of logging something in a springboard application couldn't be more simpler than they've made it let me show you so here is a spring boot app from the spring starter site that I've created starter spring that I oh I have created this with just one dependency the spring boot starter web dependency click this card if you want to know how to create a new spring boot application a spring boot project alright so I've got this project and I'm opened it in an IDE let me open the Palm D'Or XML here so here you can see that there is this one dependency that spring has created for me it's the spring boot starter web dependency it's nothing fancy really it's just a web dependency that allows this project this application to act as a web app remember this dependency okay we'll come back to that in just a minute alright so now I want to demonstrate some logging and I need a place to add Jalad messages so I'm going to create the simple rest controller and request map it to the root URL and I'm going to return a string here which just lets us know that this controller was called when we access that URL and of course I need to add an annotation here to make this arrest controller and now we have a simple risk controller I can start the app and I can access this in the browser all right so I'm going to start this as a Java class they open the browser access localhost 8080 I get that message simple alright now on to adding logging to this controller how do I add logging the basic way to add logging is to use this class called logger factory and it has a method called get logger all right so it's a static method which takes in your class so that it knows which class is doing the actual logging and then you get a logger object out of it all right so I'm going to call this logger factory don't get logger in order to get the slogger object the Slugger factory don't get logger is a standard way for you to log in Java using this framework called SL f4j it's almost like a de facto standard for logging in Java so it works pretty much the same way that you use a silver J because it's available to you and then you use this logger object to just log something I'm gonna log I'm gonna do a logger dot error here and then I'm gonna pass in a string I never happened of course no error has happened here I'm just doing this for demonstration purposes alright so I'm gonna save restart this project and then open this in the browser and if I look at the console here you see here that what happened and every time access this controller method a message gets locked all right so this is how you not simple default out-of-the-box behavior for logging now you might be wondering what's doing the actual logging if you know SL f4j is pretty much like an interface and it doesn't do the actual logging you need a logging library for logging those messages so what's happening here so what happens is again like I said Springwood has intelligent defaults it has this kind of out-of-the-box behavior for logging because it can assumes that well everybody needs logging so might as well provide it so when I added this dependency spring boot starter with them what it does is it also pulls in this dependency called spring boot starter logging I'm pretty sure all of these starter dependencies have an implicit dependency on starter logging so no matter which one you choose you are kind of getting logging out of the box and what happens when starter logging is added but what it's doing is it's actually having a dependency on this other thing called spring JCL this stands for spring commons logging bridge all right so this is these are the two dependencies which are actually causing logging functionality to be available to your project alright so that's the first thing well then what is actually doing all the work for you what is actually logging in like I mentioned Accela for J is just the interface what's the actual implementation well turns out the default implementation in springboard when you use this kind of starter dependency is the framework called log back log back is a framework which is kind of a successor to the log4j framework which is kind of pretty popular it used to be the most widely used a logging framework with log for sure but now it's been discontinued from active development and log back is kind of like a successor so if you go to log bag qos dot CH which is the home page for this log back project you can learn a little bit more about it alright and we can actually verify that your spring boot project uses log back let me show you so if you switch back to your ID and if you look at the external libraries folder get see here are the log back jars so this is what is actually doing the implementation you are calling the logging API using a celebrity but then SLO for J is delegating the logging work itself to log back which happens to be there in your class path because you used the starter dependency all right now let's look at how to configure log levels in the home method we did a logger dot error but it's not quite an error as it we want a trace we want to have some message which say is hey we reach the controller we executed the controller but it's not quite an error level so let's change that so what I'm gonna do is set this to trace all right I'm gonna have a trace message here it says home method accessed all right by default if I were to restart this application and you're on it this message is not gonna show in the console because the default logged level in your Springwood application is info and trace is not up to the info level it is it does not get logged it's it has to be in for above alright so how do you make trace show for the answer is you define a property in your property file I can add a property in my application to properties to tell this logging infrastructure to log trace messages as well and here's how you do it open the application read properties file and then here I'm gonna add a property logging that level dot root equals trace now what this is gonna do is it's gonna set the logging level for the root of your Springwood project which means it's gonna set it to everything and all of the logging levels for the whole project is going to be set to trace all right now let's restart and see what happens restarting my application and look at this craziness here look at the amount of log messages that's being rendered each time all right this is happening because we have set the entire applications log level to trace which means that even the spring logging even the spring boot and the spring frameworks logging at trace level as being here so okay we clearly need to be able to set different log levels for the framework and the library code as opposed to our application code right so thankfully you can specify a package name when setting log levels so what I'm gonna do here is I'm going to create this I'm gonna set this property as let's say I make an info which is the default and then I'm going to do logging that level dot and my package team which is I order Java brains and I'm gonna set this to trace all right now if I were to restart this only the eye order Java brains package will have trace level of logging and then the other packages will have the default level of logging all right now here you can see the trace method is being called and it's being logged but none of the other craziness is happening all right now here is a slide which talks about some of the other properties that you can set in application or properties all right so you have a bunch of stuff like logging dot file which lets you define the log file and then you can specify some of the properties of log file like Mac sighs max history the path of the log file and then you can also have what the pattern of your log messages are gonna be all right so you have a bunch of these as well all right now let's look at one more thing that you can do to make this a little bit better one thing that you will notice is as you make more and more configuration for your log properties all those property keys are going to mess up your application or properties there's gonna be a lot of confusion a lot of configuration in your property file and let's say you want to extract that out and you want to have xml configuration there is a standard xml way of configuring frameworks like log back and let's say you want to do that well you can do this by specifying a certain file name dart XML in the classpath so there are a couple of file names that spring looks at so you have log background XML which is kind of like the standard way in which you configure the log back logging framework or you can say log back - spring Datuk similar alright and this is what spring boot looks at as long as you add one of these files to the classpath that this particular name you can specify your login configuration in that XML file and Springwood is gonna look it up alright well you might be asking okay but this is 2019 do we really want to be doing XML files isn't XML verbose well turns out there is a shortcut you don't have to specify huge XML configuration you can actually do inheriting so here's an example of log back - spring XML file alright so what you have here is an include which is looking at the base start XML which is kind of like the default out of the box configuration and then what you specify in your XML file is only the override so in this case I'm specifying logger name or got Spring Framework dog web for that package I want to set the level as debug so this is a one-line configuration and I'm inheriting everything else from this based on XML file you can actually look up this file which is in your dependencies and it's gonna give you a whole lot of configuration that you can use and override if you like so this is another way in which you can configure your logging in your Springwood application alright to summarize what did we cover today logging is built-in that all of the starter dependencies with spring all right with spring boot secondly you use the logger factory that get logger to get the longer instance in other words you use the SL f4j api but what's doing the actual work there's log back by default spring boot brings in log back with those starter dependencies but this can be changed of course you can specify the configuration for your logging using application property values are you can specify XML files Springwood is gonna pick it up and it's going to use that configuration so this was how to use logging in your spring boot application I hope this was helpful please check out some of these other videos about spring and spring board thanks for watching [Music]
Info
Channel: Java Brains
Views: 230,764
Rating: undefined out of 5
Keywords: java brains, java, brains, koushik, kaushik, brainbytes, explained, var, spring, spring boot, spring boot logging, logging, logger, slf4j, logback, spring boot starter, spring-jcl
Id: lGrcZsw-hKQ
Channel Id: undefined
Length: 13min 46sec (826 seconds)
Published: Thu Jul 25 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.