Spring Boot and Logback - How to log into a rolling file

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the previous application we talked about how to create a new application using Springport since every good applications needs to have a proper login in place in this video we will talk about how to configure log back for our application we want to lock messages into two places the first place is the console and the second place will be a file we also want logback to take care of the archiving of our files so we can take a look in the future what happened in our application so let's start by creating a logger in our main class the library application we can see that we already have a few logger classes available it's because logback is coming out of the box from Spring boot so we do not need to add any new dependencies let's use the local class from the package.org.slf4j now we want to create a new instance of this logger to create a new instance of the logger we will use the locker Factory from the org slf4j package the logger Factory has a method called get Locker which takes a class as an argument the name of this class is used as the name of the logger so let's use the library application.class so we know that the Locker within the library application class was logging the message now since we have a look at in place within this class let's use this logger instead of the system outpink line here look info hello vert on this line of code we are logging the hello over message as an info let's try this out by running the application we can see the hello world log message here so it's working but this is only in console we also want to log into a file for this we need to do some configuration of the log back the configuration for log back are done using a file called logback XML this file should be stored in the resources folder from there the log back can pick it up so let's create the file logoback.xella foreign file has a configuration element as a root within this file we need to configure the appenders which are responsible for publishing the log messages into different destinations and the loggers which are responsible for collecting the messages so let's start with creating the appender for publishing the messages into the console for this I will create an appender root element it's up underneath to have a name so we can reference it since it's publishing into the console let's name it console the upender also has to have a class which says what type of upender it is we want to publish to console and for this we will use the console appender from package oh and here we have the console appender now as we created the upender we also have to configure this upender by saying how the message in this output should look like for this we create an encode element and within the encoder element we set the pattern of the message to make it faster let me quickly copy a sample button here let's take a look into this pattern this is how the message in the console will look like the first part here says that at first we are printing the date and time when the message was locked here we have the ears per month the date then the space then here we have the hours minutes and seconds the second part says the thread from where the logger was used this third part contains the logging level whether it's an info debug warning error then we have the logger name then we have a message and a new line at the end now we have the console appender ready but we need to assign this upender into some Locker there is a so-called root logger under this root logger belongs all the lockers so if we assign the console appender to this root logger we know that all the loggers will print into this append there so let's do this root and assign it assign the console appender using the appender ref element this reference name here has to be the same as the appender name also we want to say what type of log messages we want to receive what level of the log message so this can be configured using the level and we want to log messages in debug and below this means we will lock the debug the info the warning Etc now our configuration file is ready so let's try it we expect a difference in what messages we have locked in the previous start we've seen that we have only info messages now we configure the root logger to lock also debug messages so we want to see the debug messages between these info messages let's start the application and the application built successfully executed successfully let's see the log messages here we have the Hello World info message but we can also see the debug messages this means that the configuration is correct so we can print the load messages into the consumer but we also want to bring the load messages into our file for this we need to create a new appender which will print to the file and let's do this let's name this upender as a rolling file because we also want to roll it when some time is reached or some size is reached this means that for example when we run the application the next day we will create a new log file and the Old Log file will be archived so for this purpose we need to use an upender class called rolling file appender it's located in the same package and here we have it rolling file upender now this appender has a bit more configuration than the console configuration because we also need to say for example where the file will be located so let's configure the file that the log will be published to we want to use a file called application .log and we want this file to be located in the root of this project in the logs folder okay now we also need to configure how the log messages should look like within this file and actually it can look the same as in the console so let's just copy this encoder part here now we have a code duplication because we see that the pattern is the same so let's introduce a property which will store this pattern and so we can avoid the duplication let's copy the button here and we have the property so instead of having the same pattern here and here we will have it only here as if value start in the lock button property and we can reference this lock pattern for property within our console upender and also within our rolling file upender like this okay now we created the running file upender but we also need to include the this upender to the root Locker by adding the rolling file as a reference and then there is one last thing we need to do and it to configure how we want to roll these files it means well and where we want to create the archive we can configure this by setting rolling policies and for Rolling policy we will use a class called size and time based rolling policy so so let's use it this means that we will roll based on size and time now where we want to store the archive files we configure it within the file name pattern so we want to store them within the log folder we use also for the logs and within a subdirectory called archive and here we specify the name of the archive file so let's call it application.log and let's call it application and then we specified the date month and day sorry your month and day Dot Lock dot zip this button here does more than just saying the name this button here also save how we want to Archive so by this suffix dot zip we say that we want to create a zip file which will contain the load file and here by this date we are saying how often we want to Archive by specifying here the day we say that every day we want to Archive the last log files and start logging into a new file but we also want to Archive based on size so what if we reach the maximum allowed size earlier than the day finish then we need to Archive but we need to have an extra information which will identify this earlier archive and this extra information can be done by percentage e it's an increasing number which says which Archive of this day is this file so this will be the file pattern name okay it's located in the logs directory under the archive subdirectory its name starts with the application prefix it's rolling every day when we need to do more Rollings during one day it's incrementing its number and this file has a lock dot zip suffix so it will be a zip file now we need to set a few more settings because it's not just a Time based rolling policy but it's also a size based rolling policy so we need to say how big a log file can be the upper boundary we can say this by the max file size element we want to have our log files maximum 10 megabytes big and we also can add some upper boundary how many load files back we want to keep so look back we'll do the cleaning for us so we want to repeat the maximum history of 105. and that's it for the rolling file configuration let's try now I'm running the application and let's see what happens the application finished we have the logs in the console but we can see that we also created a logs directory and this logs directory has an application log file where we have the locks now I want to show you also how this archiving look likes so let me update the max file size to 10 kilobytes instead of 10 megabytes this means that when the application log file reaches the 10 kilobytes size we will archive it and create a new application log file for the new logs so let's start the application and as we can see we have the archive directory and there's a zip file within this Archer directory the zip file is called application it's the log from date 2022 the December of 26 and it's the increment number of zero so so it's the first zip file from this day and then we have a new log file created so the logging of our application is in place we are logging into a file we are logging into a console and that's it for today's video
Info
Channel: The Java Mindset
Views: 1,609
Rating: undefined out of 5
Keywords: Java, Spring Boot, Spring, Spring Initializr, Web application, Programming, Web app, WebApp, TheJavaMindset, The Java Mindset, Object oriented programming, OOP, Java developer, Programming language, Logback, SLF4J, logging, Rolling file, RollingFile, logback.xml, tutorial
Id: OoD4JFjwE9o
Channel Id: undefined
Length: 19min 55sec (1195 seconds)
Published: Tue Dec 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.