Get Started Quickly With Java Logging
Tags: logging, java
Published: 2020-05-05 15:26:53

<!-- wp:paragraph -->
<p>You've already seen how to <a href="https://www.scalyr.com/blog/get-started-quickly-csharp-logging/">get started with C# logging as quickly as possible.</a>&nbsp; But what if you're more of a Java guy or gal? Well, then we've got your back, too: today's post will get you up to speed with logging using C#'s older cousin, Java.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As in the previous post in this series, we'll not only provide a quick guide but also go into more detail about logging, diving particularly into the <em>what</em> and <em>why</em> of logging.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>So, let's <span style="text-decoration: underline;"><strong>get you started with Java logging as quickly as possible</strong></span>.&nbsp; When you're finished reading, you'll know how to approach logging in your Java codebase.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":704,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://library.scalyr.com/2018/07/19175316/Java-Logging-Icon-254x300.png" alt="" class="wp-image-704"/></figure></div>
<!-- /wp:image -->

<!-- wp:more -->
<!--more-->
<!-- /wp:more -->

<!-- wp:heading -->
<h2>The Simplest Possible Java Logging</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>For this simple demo, I'm going to use the free&nbsp;<a href="https://www.jetbrains.com/idea/download/#section=windows">community version of IntelliJ IDEA</a>. I'm also assuming that you have the&nbsp;<a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html?ssSourceSiteId=otnpt">Java JDK</a>&nbsp;installed on your machine.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>First, open IntelliJ IDEA and click on "Create New Project":</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":5434,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://library.scalyr.com/2019/09/26142950/01-newproject.png" alt="" class="wp-image-5434"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>On the next screen, select "Java" as the type of project, on the left panel. You'll also need to point to the JDK's path, in case you haven't yet.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":2451,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://library.scalyr.com/2018/01/30180513/newproject2.png" alt="" class="wp-image-2451"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Third step---mark the "Create project from template" option and then select "Command Line App":</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":2452,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://library.scalyr.com/2018/01/30180532/commandline.png" alt="" class="wp-image-2452"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>I've called my project "JavaLoggingDemo," as you can see from the image below.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":2453,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://library.scalyr.com/2018/01/30180551/javaloggingdemo.png" alt="" class="wp-image-2453"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>After doing the steps above, you should see the Main class that was automatically created for you. It should look like this:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>package com.company;

public class Main {

    public static void main(String[] args) {
    // write your code here
    }
}
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Let's get to work. Replace the "// write your code here" comment with the following line:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>System.out.println("Hello there!");
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Now you have an application that displays a friendly greeting.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Next, let's run our app. Go to&nbsp;<code>Run</code>&nbsp;&gt;&nbsp;<code>Run 'Main'</code>&nbsp;or use the shortcut&nbsp;<code>Shift</code>&nbsp;+&nbsp;<code>F10</code>&nbsp;(Windows and Linux) or&nbsp;<code>Control</code>&nbsp;+&nbsp;<code>R</code>&nbsp;on Mac.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If everything went well, you should see something like this:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":2454,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://library.scalyr.com/2018/01/30180619/img5wentwell.png" alt="" class="wp-image-2454"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Nice. But let's say the requirements for our little app changed, as they always seem to do. Now we need our app not only to display a message but also to log it somewhere. How to go about that?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There are a lot of sophisticated options we could use, but let's try and do the simplest thing we can. Edit the code in your&nbsp;<code>main</code>&nbsp;function so it looks like this:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>public static void main(String[] args) throws IOException {
    String message = "Hello there!";
    System.out.println(message);
    Files.write(Paths.get("log.txt"), message.getBytes());
}
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Notice the changes. We've added a new variable (<code>message</code>) to store the greeting's text. Then there's the printing. And finally, there's a new line, which is meant to save the text to a file.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We also had to add a throw declaration to our method since the last line can throw an&nbsp;<code>IOException</code>. Additionally, we've added some new import declarations. The whole code now looks like this:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>package com.company;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Main {

    public static void main(String[] args) throws IOException {
        String message = "Hello there!";
        System.out.println(message);
        Files.write(Paths.get("log.txt"), message.getBytes());
    }
}
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>When you run your application again, it should behave the same as before. But the last line added will create a log file for you. It'll be located in your project folder:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":5435} -->
<figure class="wp-block-image"><img src="https://library.scalyr.com/2019/09/26143113/02-loggggg.png" alt="" class="wp-image-5435"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>That's pretty much it. You've just written your first Java logger! And with that first, small step, you're ready to try larger ones.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>What Is Application Logging?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>First things first. Let's briefly define what application logging is.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>According to&nbsp;<a href="https://en.wikipedia.org/wiki/Log_file">Wikipedia</a>,</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p>In computing, a log file is a file that records either events that occur in an operating system or other software runs, or messages between different users of a communication software. Logging is the act of keeping a log.</p></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>A little bit vague, right? I tend to prefer&nbsp;<a href="https://www.scalyr.com/blog/get-started-quickly-csharp-logging/">the definition we gave in our C# logging post</a>:</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p>Application logging involves recording information about your application’s runtime behavior to a more persistent medium.</p></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>With the <em>what </em>out of the way, let's get to the <em>why</em> of logging.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>What’s the Motivation for Logging?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>I think the key part of the logging definition we've presented in the previous section was the word <em>persistent</em>. Why would we need to record information about the behavior of our app in a persistent medium?&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Well, for the same reason we record anything to a persist medium: so we can get back to it later. The question then becomes "Why would you want to do that with events that happened in an application execution?"</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It all boils down to the nature of software itself.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A piece of software in production is a very complicated thing, more often than not running in a totally non-controlled environment. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>How do you know it's going to work? And when things go wrong, how can you know what exactly went off the rails?</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":5436,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://library.scalyr.com/2019/09/26144203/Hope-isnt-a-strategy.-Logging-is.png" alt="Hope isn't a strategy. Logging is!" class="wp-image-5436"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>You can always cross your fingers and hope, but as they say, hope is not a strategy. Logging is, though!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In a nutshell, you use logging so that you can after-the-fact debug your application. By <a href="https://www.scalyr.com/blog/be-kind-to-your-log-file-and-those-reading-it/">reading the log entries</a>, you can understand the events that happened with your application, or even retrace the steps taken by a user, in order to diagnose and fix issues.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Evolving Our Approach: What Should We Capture? And To Where?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Being able to do an after-the-fact investigation on your app sounds like a great benefit --- and, indeed, it is. Our humble logger isn't able to provide us this benefit, though, since all it can do is write some text to a file.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Our job now should be to evolve our logger, turning it into a richer source of information for its future readers.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><span style="text-decoration: underline;"><strong>So how do we do that?</strong></span></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The key point to understand is the nature of a log entry. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Some of this is <a href="https://www.scalyr.com/blog/get-started-quickly-csharp-logging/">familiar ground</a>, but I'm going to recap here for completeness' sake. Think of a log entry as an&nbsp;<em>event.</em>&nbsp;Something that is of interest to your application happened in a given instant in time. So the value of your log entry derives from the data you capture about the event.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>The following list contains some examples of things that you'll probably want to capture.</h4>
<!-- /wp:heading -->

<!-- wp:list {"ordered":true} -->
<ol><li>A timestamp. Exactly when did the event take place? (You'll want to record times in UTC and using a standard format such as&nbsp;<a href="https://en.wikipedia.org/wiki/ISO_8601">ISO-8601</a>, in case you're logging to a file.)</li><li>Context that makes it clear to a reader what the entry's about. Just recording “Hello, user!” might prove confusing weeks or months later. A better message might say, “Recorded user greeting of ‘Hello, user!'”</li><li>Tags and/or categories for each entry, to enable later search and classification.</li><li>Log levels, such as “error,” “warning,” or “information,” that allow even more filtering and context.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>After deciding what things you want to capture, the next step is defining where you want to log to. Even though I've used a file in our first example, there's nothing stopping you from logging to other media, such as a database (relational or&nbsp;<a href="https://en.wikipedia.org/wiki/NoSQL">not</a>), the&nbsp;<a href="https://en.wikipedia.org/wiki/Event_Viewer">windows event viewer</a>, or even the console.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Enter the Java Logging Framework</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Time to get back to our demo. Here's the thing: it was designed to give you a quick start on the Java&nbsp;logging world. But now you're equipped to look at it in a more critical way, so let's do just that.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As it turns out, our humble logger is inadequate in several important ways.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>For one thing, the code always overwrites the log file. A log strategy that only allows you to see the last event isn't terribly useful, isn't it?</li><li>And since we're talking about overwriting the file, it probably wouldn't hurt to think about file permissions, how to deal with concurrent access, and that sort of thing.</li><li>Finally, it sounds like an awful lot of work having to write boilerplate code in each logging call to get log levels, timestamps, and all that jazz I mentioned in the previous section.</li><li></li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>I could go on and on, but you've got the picture: coming up with a sound Java logging approach requires a lot of thought. I have good news, though. As it turns out, people have already solved these problems. Not only that, they provide their solutions for you, ready to use and often for free<em>,</em>&nbsp;in the form of a&nbsp;<a href="https://www.scalyr.com/blog/logging-framework/">logging framework</a>. </p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":5437,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://library.scalyr.com/2019/09/26144338/a-sound-Java-logging-approach-requires-a-lot-of-thought.png" alt="a sound Java logging approach requires a lot of thought" class="wp-image-5437"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>A logging framework is a package you install in your application that provides easy and configurable logging for you. With a little bit of configuration, you can make one-line calls as simple as the one in our toy example. But this time, they'll have consistent and nicely formatted log entries, and they'll have answers to all those questions from the previous section.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Installing a Logging Framework</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The first thing we're going to do is go back to our codebase and delete that line that writes to the file. Then we can proceed to install a logging framework called&nbsp;<strong>log4j</strong>. This is only one of the options that are out there, and I do encourage you to try alternatives later. But since this is a quick guide, it makes sense to go with log4j since it's a widely used framework.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>On the&nbsp;<code>Project</code>&nbsp;tool windows, right-click on the "JavaLoggingDemo" module and then on "Add Framework Support...":</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":2457,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://library.scalyr.com/2018/01/30180827/frameworksupport.png" alt="" class="wp-image-2457"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Now, select "Maven" and click OK:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":2458,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://library.scalyr.com/2018/01/30180845/maven.png" alt="" class="wp-image-2458"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>After you've done this, the IDE will create a&nbsp;<code>pom.xml</code>&nbsp;file for you and open it for editing:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":2459,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://library.scalyr.com/2018/01/30180901/pomxml-1024x228.png" alt="" class="wp-image-2459"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Now, copy and paste the following XML text into your file:</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted {"className":"lang:default decode:true"} -->
<pre class="wp-block-preformatted lang:default decode:true">&lt;dependencies&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
        &lt;artifactId&gt;log4j-api&lt;/artifactId&gt;
        &lt;version&gt;2.10.0&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
        &lt;artifactId&gt;log4j-core&lt;/artifactId&gt;
        &lt;version&gt;2.10.0&lt;/version&gt;
      &lt;/dependency&gt;
    &lt;/dependencies&gt;</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>The whole file should look like this by now:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&lt;?xml version="1.0" encoding="UTF-8"?>
&lt;project 
         
         xsi_schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    &lt;modelVersion>4.0.0&lt;/modelVersion>

    &lt;groupId>groupId&lt;/groupId>
    &lt;artifactId>JavaLoggingDemo&lt;/artifactId>
    &lt;version>1.0-SNAPSHOT&lt;/version>
    &lt;dependencies>
        &lt;dependency>
            &lt;groupId>org.apache.logging.log4j&lt;/groupId>
            &lt;artifactId>log4j-api&lt;/artifactId>
            &lt;version>2.10.0&lt;/version>
        &lt;/dependency>
        &lt;dependency>
            &lt;groupId>org.apache.logging.log4j&lt;/groupId>
            &lt;artifactId>log4j-core&lt;/artifactId>
            &lt;version>2.10.0&lt;/version>
        &lt;/dependency>
    &lt;/dependencies>     
&lt;/project>
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>After adding the dependencies to the file, you should see a small pop-up in the bottom-right corner of your screen. Click on "Import Changes" to finish the installation.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":2460,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://library.scalyr.com/2018/01/30180926/installation-1024x408.png" alt="" class="wp-image-2460"/></figure></div>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>Configuring the Logger</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now it's time to <a href="https://www.scalyr.com/blog/log4j2-configuration-detailed-guide/">configure log4j</a>. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>What I'm going show you is&nbsp;<em>one</em>&nbsp;way of doing this; it's not the only one, and it's probably not even the best one, whatever "best" may mean. But it's definitely shorter than a lot of the tutorials you see out there.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Pretty much all you have to do is to create a configuration file and then write some lines of code. Don't believe me? Well, let me just show you, then.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>First, go to the "Project" tool window on IntelliJ. Expand the folders and locate the "resources" folder, under&nbsp;<code>JavaLoggingDemo</code>&nbsp;&gt;&nbsp;<code>src</code>&nbsp;&gt;&nbsp;<code>main</code>, just like the image below:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":2461,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://library.scalyr.com/2018/01/30180944/javademo.png" alt="" class="wp-image-2461"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Now, right-click on the resources folder, and then select&nbsp;<code>New</code>&nbsp;&gt;&nbsp;<code>File</code>:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":2462,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://library.scalyr.com/2018/01/30181002/newfile.png" alt="New File Screenshot" class="wp-image-2462"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>When you're prompted for a name, enter "log4j2.xml." After the file is created, paste the following text into it:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&lt;?xml version="1.0" encoding="UTF-8"?>
&lt;Configuration status="INFO">
    &lt;Appenders>
        &lt;File name="FileAppender" fileName="proper.log" immediateFlush="false" append="true">
            &lt;PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        &lt;/File>
    &lt;/Appenders>
    &lt;Loggers>
        &lt;Root level="ALL">
            &lt;AppenderRef ref="FileAppender"/>
        &lt;/Root>
    &lt;/Loggers>
&lt;/Configuration>
</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2>Editing the Code</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>First, at the top of the file, add the following two import declarations:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Then, add the following line to the top of the&nbsp;<code>Main</code>&nbsp;class, which declares a private field to hold the instance of the logger:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>private static final Logger logger = LogManager.getLogger(Main.class);
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Finally, add the following line of code to where you earlier had the call to&nbsp;<code>Files.write(Paths.get("log.txt"), message.getBytes())</code>:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>logger.info(message);
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>The whole class should now look like this:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>package com.company;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

public class Main {

    private static final Logger logger = LogManager.getLogger(Main.class);

    public static void main(String[] args) {
        String message = "Hello there!";
        System.out.println(message);
        logger.info(message);
    }
}
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>And we're done! Now all you have to do is run the application.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Checking the Results</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now navigate back to the application folder and notice a new file there, called "proper.log." If you open it using a text editor, you should see the following:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>2019-09-25 15:39:29.739 [main] INFO  com.company.Main - Hello there!
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Let's break this line into its components.</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p><code>2017-12-31 15:39:29.739</code></p></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>First, we have the timestamp, in the ISO-8601-compliant format.</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p><code>[main]</code></p></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>This refers to the name of the thread from which the event originated.</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p><code>INFO</code></p></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>Here we have the logging level.</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p><code>com.company.Main</code></p></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>Then, the name of the class.</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p><code>Hello there!</code></p></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>Last, but not least, the message itself.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>The Power of a Java Logging Framework</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>I think you'll agree that our logger just got a lot more useful with the update we've made.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Just by creating a config file and writing a few lines of code, we were able to configure a realistic example of a logger. This isn't all there is to it, of course, but it's already enough to be useful in a real application. What's more, it gives you a pretty good idea of the power a logging framework can put into your hands.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":5440,"align":"center","width":350,"height":438} -->
<div class="wp-block-image"><figure class="aligncenter is-resized"><img src="https://library.scalyr.com/2019/09/26144926/hand-holding-light-bulb.jpg" alt="hand holding light bulb" class="wp-image-5440" width="350" height="438"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The XML configuration file is one the places this power really manifests itself.&nbsp; It offers you a lot of options to configure each entry that you log, such as the name of the log file or if the logger should append to it or overwrite it, just to name a few.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A lot of the flexibility log4j provides is due to something called an&nbsp;<a href="https://scalyr.com/blog/log-appender-what-is-it-and-why-would-you-use-it/">appender</a>.&nbsp; The appender is the component that effectively writes the message to some medium, and there are many of them available. This means it's possible to direct your logs somewhere else entirely, just by adding a new appender to the XML file, without even touching your application code.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Separation of concerns at its finest, if you ask me.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":5438,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://library.scalyr.com/2019/09/26144503/Log4j_With_Scalyr_Colors2-300x300.png" alt="" class="wp-image-5438"/></figure></div>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>Happy Learning!</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>What we've seen today is just the tip of the iceberg. Make no mistake: you now have a lot of learning ahead of you. But you have the advantage of already having a live, functional, and realistic setup to work with. Start by playing with and tweaking it. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here are some tips on what you can do:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Learn how to configure different loggers, one for each log level.</li><li>Research and try out the different appenders available.</li><li>Play with the options available for the "layout" entry.</li><li>Learn about the <a href="https://logging.apache.org/log4j/2.x/manual/configuration.html">other ways</a> you can configure log4j.</li><li>You have&nbsp;lots of options for logging frameworks; try some of them, once you're confident enough with log4j.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>And to learn more about Java&nbsp;logging and <a href="https://www.scalyr.com/blog/application-logging-practices-adopt/">logging strategies in general</a>, you probably won't find a better place then Scalyr's<a href="https://scalyr.com/blog/"> blog</a>. Scalyr offers a&nbsp;<a href="https://www.scalyr.com/blog/what-is-log-aggregation-and-how-does-it-help-you/">log aggregation tool</a>, which means that once you have lots of log files and data, they’ll help you organize, search, and make sense of all these data. So stay tuned for more!</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":5439,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://library.scalyr.com/2019/09/26144621/Now-its-up-to-you..png" alt="Now it's up to you." class="wp-image-5439"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Learning something from zero can be an overwhelming task. Fortunately for you, you don't have to do that. We've given you the fundamentals upon which you can build a solid and long-lasting body of knowledge.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now it's up to you.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>PS: If you're interested in further reading on the topic of logging in other languages and platforms, take a look at some of our available guides:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><a href="https://www.scalyr.com/blog/getting-started-quickly-ruby-logging/">Ruby Logging Guide</a></li><li><a href="https://www.scalyr.com/blog/getting-started-quickly-node-js-logging">Node.Js Logging Guide</a></li><li><a href="https://www.scalyr.com/blog/rails-logger/">Ruby On Rails Logging Guide</a></li><li><a href="https://www.scalyr.com/blog/getting-started-angular-logging/">Angular Logging Guide</a></li><li><a href="https://www.scalyr.com/blog/started-quickly-python-logging/">Python Logging Guide</a></li><li><a href="https://www.scalyr.com/blog/getting-started-quickly-c-logging/">Logging Guide</a></li><li><a href="https://www.scalyr.com/blog/getting-started-quickly-kotlin-logging/">Kotlin Logging Guide</a></li><li><a href="https://www.scalyr.com/blog/getting-started-quickly-laravel-logging/">Laravel Logging Guide</a></li></ul>
<!-- /wp:list -->