48 - Logging ( FileHandler; ConsoleHandler; Levels ) | Java Tutorials
alright let's talk about logging in Java
so if you want to save certain events or
errors that happen while your code is
running and then be able to look back at
them later this definitely something you
should incorporate now Java does have a
logger and it's in Java util dot logging
and of course there are third-party
loggers that do a more efficient job
have extra methods and all that stuff so
you can check those out if you want to
but how do you use this one how you use
just a basic one well I have a class
here and I want to be able to use my
logger throughout the entire class so
let's put it up here and I want to make
this private because no one else needs
to have access to this and let's go
ahead and make it also final and Static
so we don't have to mess with anything
the class again it's called logger and
then give your logger a name I'm just
going to call it L o G or normally you
do all caps for final things but anyways
let's do logger dot get logger so in
here you can put a name for your logger
and if the name you put in you already
exists then it will just use that one so
this will automatically create one if it
doesn't exist and usually people name
their loggers after their class name
just to keep things easier especially if
your lawgiver has a certain setup for
each class this might be useful and a
lot of times you'll see people do this
instead they'll put the class name dot
class and then finally get name and this
does the same thing but it's going to
get the full name of the class if it's
in like a package so for me this is just
going to all return log example but if
it's in a package say like up here then
instead of just saying the class it
would say Java dot util dot logging dot
and then put the class name if it was in
that package a lot of times if it's a
really small project what people will
just use the global logger so everything
will be glued out as global so let's
just use this one again you usually use
the class name and let's make a simple
log so type out your logger name and
then you have a bunch of methods of
course here well
we're going to make a log and you have
to choose the level of this log so
basically how important is it on the
scale let's just do an info one for this
round and put in a message let's say now
we can compile this and run it and
you'll see this one prints out to the
screen now normally your loggers won't
do anything they actually have to have a
handler on to them so that you can
choose what to do with your log so if
you want to print them out to the screen
like it's doing here or if you want to
print them to a file like we're about to
do now the only reason this is printing
out to the screen now even though I
don't have a handler for this yet is
because it's passing it up to the route
logger and that automatically has a
little console handler onto it now every
logger as well as the handlers to them
you can set a custom level level cutoff
so what they will actually log out so
all the levels by the way are right here
and there in order so this one being
just something that's kind of not very
important that you might want to just
take a note of all the way up to
something that's very severe and that
you definitely want to make sure it's
logged out or handled so you have these
different levels and each time you make
a log take into account what level you
should probably put it on to this log
doesn't have a handler or anything so
it's passing up to the root one and it
has a cut-off level of info that means
only these three will be logged out to
the console so if I were to change this
to say a fine message then when I run
this notice nothing shows up and again
you know the levels are just to help
organize things but also in certain
levels of development you might want to
turn specific levels off in the
beginning you might want to let
everything pass through but then maybe
in production you only want it severe
wants to be logged out or warning etc
you you get the idea so the way to set
that is to do logger set level and now
you can choose of course what level you
want to let through so if I set this at
fine then that would mean everything
from here up would be logged now even if
you change this level it's not going
to effect what's logging out right now
because that's being handled by the
route logger we don't actually have any
kind of handle on this so the first
thing I like to do is get the log
manager and do reset and this will get
rid of any handlers that the route one
has which is this console one right here
that way it's not interfering with any
of my stuff I think there's also a
method onto this where you can say not
to pass up to the parent etc anyways so
now what I like to do is set the level
of my logger and I like to do it as all
so that everything is passed through and
then all I have to do now is set this to
off if I want nothing to pass through so
this is kind of like a global switch to
affect all your other handlers we're
going to have to make a handler of some
kind so there's two of them that this
package includes and it's the console
one and a file one and you can have
multiple handlers for the same logger so
let's do that console Handler and let's
just call this CH equals new console
Handler and now we want to set the level
of this as well so what do I actually
want to be displayed in the console
especially if this is a console app and
my users are interacting through I don't
really want to bother them with any kind
of basic small logs because that would
interfere with their experience right
your logger should be in the background
and no one should notice them again
though it's completely up to you how you
handle all this stuff so now let's get
the logger and add this handle to it by
doing CH because that is the variable we
created right here and now any logs I
make if they are below severe so any of
these they won't show up and by the way
this the logger lets you output also
like your exception by doing another
comma and then putting your exception
here so if you just want to display a
message then there's a shortcut all of
these have methods with their name so if
I wanted to do an info log then instead
of using log and then putting the level
you can just use the method associated
with that level again we can add
multiple handlers so let's also add a
file one so that we can log out a file
so let's use the file Handler and I'm
just going to call this F H new file
Handler and now give your log a name I
like to put the class name as well here
but you can call this what you like so
I'm just going to call it my laundry
love you can set a formatter for this so
if you want it to be simple like a
simple formatter you could do new simple
formatter here we go and this will make
your logs look like this one here and
you can create your own formatter so I'm
not going to get into that obviously
that's a whole nother subject but I'm
going to just keep the default which is
to output it as XML and you'll see that
in just a minute
so now let's set the level of this so
what do I want to allow to be logged out
into my file and again depending on how
your program works and what you want
logged out then you can choose that so
I'm just going to make let's say
everything above fine let's also add
that handler to my logger so ad handler
SH and now we have both a console
handler and a file Handler and they each
have different cutoff levels of what
they will actually log out and I forgot
we're dealing with file so we will have
some IO exceptions here so what I'm
going to do is just wrap my entire file
handler in a try-catch block that way if
it fails I'm going to continue on with
my program I don't really want to stop
my program just because my logger
couldn't start up and then maybe we want
to log out this at least to the console
level severe so that it actually makes
it to our console because I do have that
cut off and let's just say and then I
can also display the exception by just
putting a here or you could also put a
specific part of the exception like the
message of it by doing any of its method
that it has that way at least now we
know that it failed and we can check
into that if you look I have a logger
file called my logger and in that
displays our log so it will say of
course we chose to use the global a
logger then it will say you know the
level class the method that the log is
coming from the message of it etc let's
move all this into another method
because it is kind of cluttery set up
logger and paste that in there so then
at the top of my main method all I have
to do now is just put my class name of
course this is a static context so put
my class name and then call my setup and
compile this and nothing shows up there
but in our logger we can see both of our
logs here alright so a couple things to
note before I go your logo will
overwrite all the contents that it had
before so if you want to instead append
to that file then you can put a comma
true here on your file handler which
just means to append to and I'm too lazy
to think of code that would cause an
error so let's throw a new let's just do
i/o exception we could even put a
message in here all right and we're
going to catch this and log it out
so in here let's do our logger let's use
logs I'm going to call this severe some
kind of message here for me and let's
put just the exception here and I keep
forgetting to include the full path to
that exception all right so now you can
see we get a severe file read error and
then it says the exception and if we
look at our log you can also see it down
here it will include now the exception
so that's an example of that also let's
do a quick example of using it in
another class in order to do this we
have to of course get a logger and I'm
going to use use the same one that we
did here because my setup that's fine
I'm fine with using that throughout
everything so by using of course the
same name that we'd out there which is
the global longer
then it will get that law grants that
have created so all our setup should
still be the exact same as the previous
one so now I'm from another class call
this method so let's do test test and
compile and run this code so now we get
our air that we specified to do and if I
look at my logger you can see that my
logs are coming from the log example
class put down here it this one comes
from the test class
I feel like this video has gotten way
too long for anyone to still be here but
if you are that is the basics of logging
things I said I would talk about writing
to files as well but I'm going to save
that for another video thanks for
watching