The problem is that the other code most likely uses a different logger. With java.util.logging you can configure the root-logger right when the application starts and the other loggers will inherit the configuration.

private static void configureRootLogger() {
    try {
        FileHandler fh = new FileHandler("MyLogFile.log");
        fh.setFormatter(new SimpleFormatter());
        Logger.getLogger("").addHandler(fh);
    } catch (IOException e) {
        // handle exception
    }
}

Full example:

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class LogToFileExample {

    private static final Logger logger = Logger.getLogger("MyLogA");

    public static void main(String[] args) {
        configureRootLogger();
        logger.info("local test message");
        final LogTester tester = new LogTester();
        tester.logMessage();
    }

    private static void configureRootLogger() {
        try {
            FileHandler fh = new FileHandler("MyLogFile.log");
            fh.setFormatter(new SimpleFormatter());
            Logger.getLogger("").addHandler(fh);
        } catch (IOException e) {
            logger.warning("Could not add handler to log to file");
        }
    }
}
import java.util.logging.Logger;

public class LogTester {

    private static final Logger logger = Logger.getLogger("MyLogB");

    public void logMessage() {
        logger.info("external log message");
    }
}

Identical output in file and console:

May 28, 2021 9:44:50 AM org.testing.LogToFileExample main
INFO: local test message
May 28, 2021 9:44:50 AM org.testing.LogTester logMessage
INFO: external log message
Answer from Matt on Stack Overflow
🌐
Medium
medium.com › javarevisited › 14-good-java-tips-for-log-printing-88f693dee65b
14 Good Java Tips for Log Printing | by omgzui | Javarevisited | Medium
March 27, 2023 - 14 Good Java Tips for Log Printing 1. Select the appropriate log level There are five common log levels, namely error, warn, info, debug, and trace. In daily development, we need to choose the …
Top answer
1 of 2
1

The problem is that the other code most likely uses a different logger. With java.util.logging you can configure the root-logger right when the application starts and the other loggers will inherit the configuration.

private static void configureRootLogger() {
    try {
        FileHandler fh = new FileHandler("MyLogFile.log");
        fh.setFormatter(new SimpleFormatter());
        Logger.getLogger("").addHandler(fh);
    } catch (IOException e) {
        // handle exception
    }
}

Full example:

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class LogToFileExample {

    private static final Logger logger = Logger.getLogger("MyLogA");

    public static void main(String[] args) {
        configureRootLogger();
        logger.info("local test message");
        final LogTester tester = new LogTester();
        tester.logMessage();
    }

    private static void configureRootLogger() {
        try {
            FileHandler fh = new FileHandler("MyLogFile.log");
            fh.setFormatter(new SimpleFormatter());
            Logger.getLogger("").addHandler(fh);
        } catch (IOException e) {
            logger.warning("Could not add handler to log to file");
        }
    }
}
import java.util.logging.Logger;

public class LogTester {

    private static final Logger logger = Logger.getLogger("MyLogB");

    public void logMessage() {
        logger.info("external log message");
    }
}

Identical output in file and console:

May 28, 2021 9:44:50 AM org.testing.LogToFileExample main
INFO: local test message
May 28, 2021 9:44:50 AM org.testing.LogTester logMessage
INFO: external log message
2 of 2
0

Instead of using the statement e.printStackTrace(), let try a statement like this logger.log(Level.ALL, e.getMessage(), e) in your catch blocks.

For example,

import java.util.logging.Level;
import java.util.logging.Logger;

...
    catch (SecurityException e) {  
                logger.log(Level.ALL, e.getMessage(), e);
                
    } 
    catch (IOException e) {  
                logger.log(Level.ALL, e.getMessage(), e);
    }  

You also can try Log4J

Discussions

java - how to print log.info message in log file - Stack Overflow
I need to know how to print a log.info message. I have no knowledge about using loggers. But I tried this code using netbeans and this didn't work. here is the code: public class login { Logge... More on stackoverflow.com
🌐 stackoverflow.com
April 27, 2017
A guide to logging in Java
A well-written article, neatly explained. I remember the bewilderment of log4j vs slf4j vs whatever-else-logger when i started out. Thank you. More on reddit.com
🌐 r/programming
34
84
September 13, 2018
docker-java: How to get container logs?
Containerized applications usually log to stdout, so you will be able to the the output with the ‚docker logs‘ command. More on reddit.com
🌐 r/docker
1
1
June 22, 2023
How can I turn off logging in SBT Test?
Those DEBUG look like they are coming from the tests themselves and not SBT. So, if you want them to go away you will probably have to turn down logging in the application. More on reddit.com
🌐 r/scala
2
3
March 17, 2018
Top answer
1 of 6
41

See this short introduction to log4j.

The issue is in using System.out to print debugging or diagnostic information. It is a bad practice because you cannot easily change log levels, turn it off, customize it, etc.

However if you are legitimately using System.out to print information to the user, then you can ignore this warning.

2 of 6
12

If you are using System.out|err.println(..) to print out user-information on console in your application's main()-method, you do nothing wrong. You can get rid of the message via inserting a comment "//NOPMD".

System.out.println("Fair use of System.out.println(..).");// NOPMD

There is a "Mark as reviewed"-Option in the PMD-Violations Outline for this purpose.

Of course you can trick PMD with following code snippet:

PrintStream out=System.out;
out.println("I am fooling PMD.");  

Outside of your main()-Method use a Log-System like eg Log4j.

UPDATE:

You can also modify the PMD-Rule "SystemPrintln" to use the following XPath:

//MethodDeclaration[@MethodName!="main"]//Name[
starts-with(@Image, 'System.out.print')
or
starts-with(@Image, 'System.err.print')
] | //Initializer//Name[
starts-with(@Image, 'System.out.print')
or
starts-with(@Image, 'System.err.print')
]

This will ignore System.out.println etc in any method named 'main' in your code, but check for System.out.println in initializer code. I like this, because from my point of view, System.out.println is safe in method 'main(String args[])'. But use with caution, I have to check, where in the AST a System.out.println can occur also and have to adapt the XPath.

🌐
Silicon Cloud
silicloud.com › home › how to print logs in java?
How to print logs in Java? - Blog - Silicon Cloud
March 21, 2024 - In Java, you can print logs using the following methods: Print logs using the System.out.println() method. Println(“Log content”); Print logs using the Java.util.logging.Logger class. Create a Logger object using the Java Util Logging package, set it to the name of your class, and then ...
🌐
Wavemaker
docs.wavemaker.com › print logger statement
Printing Logger Statement in Java Service using SLF4J | WaveMaker Docs
September 1, 2023 - To know more about the log4j2.xml file, see Enabling logs. Find the existing logger statement in your Java service, such as logger.debug("Statement").
🌐
DigitalOcean
digitalocean.com › community › tutorials › logger-in-java-logging-example
Java Logger: Logging Examples with Log4j & SLF4J | DigitalOcean
August 3, 2022 - Implement logging in Java with Logger, Log4j, and SLF4J. Complete guide covering configuration, log levels, best practices, and real-world examples.
🌐
Edureka
edureka.co › blog › logger-in-java
Logger in Java | Java Logging Basics with Log4j and util | Edureka
July 5, 2024 - Before, we deep dive into logging in java, let us understand the need for logging. While building applications, we often face errors which have to be debugged. So, with the help of logs, we can easily get information about what is happening in the application with a record of errors and unusual circumstances. Now, it might strike your mind that, why not use the System.out.print() statement in Java.
Find elsewhere
🌐
FirstMnCsa
firstmncsa.org › 2018 › 12 › 09 › debugging-print-statements-and-logging
Debugging: print statements and logging | FirstMnCsa
July 22, 2021 - For basic debugging, the console ... than the Log File Viewer. Print statements help answer questions like “Did a specific routine even execute” or “How did the motor speed vary during autonomous”. When something goes seriously wrong in a Java program, the program may communicate this to other parts ...
🌐
Loggly
loggly.com › home › java logging basics
Java Logging Basics - The Ultimate Guide To Logging - Loggly
June 27, 2025 - This section presents Java logging basics, including how to create logs, popular logging frameworks, how to create some of the best log layouts, and how to use appenders to send logs to various destinations, as well as advanced topics like thread context…
🌐
Sematext
sematext.com › home › blog › java logging tutorial: basic concepts & examples to help you log efficiently
Java Logging Tutorial: Configuration Examples to Get Started
March 19, 2025 - You can see that we’ve set up the handler which is used to set the log records destination. In our case, this is the java.util.logging.ConsoleHandler that prints the logs to the console.
🌐
Baeldung
baeldung.com › home › logging › system.out.println vs loggers
System.out.println vs Loggers | Baeldung
January 25, 2024 - When we handle exceptions in our code, we often need to learn what exceptions actually occurred at runtime. There are two common options for this: printStackTrace() or using a logger call.
🌐
Marco Behler
marcobehler.com › guides › java-logging
How To Do Logging In Java
December 9, 2020 - It also comes with a ton of different ... them to the database) and many more. Also it gives you a fair amount of control over what exactly a log message should look like, with the help of PatternLayouts. So, the same log event in your Java application, could be printed out in a log ...
🌐
Java Code Geeks
javacodegeeks.com › home
Java Logging Tutorials - Java Code Geeks
March 6, 2023 - Log4j ConsoleAppender Configuration ... to print the logs in the application console using the Log4j logging services. Log4j Conversion Pattern Example In this tutorial, I will show you how to implement some useful Log4j Conversion Patterns for writing the logging mechanism in Java ...
🌐
Coralogix
coralogix.com › home › java logging guide: how to do it right
Java Logging Guide: How To Do It Right - Coralogix
June 29, 2025 - E.g – Print these two logs together, the first log for humans and the second for computers: “transaction was completed successfully” + transactionID “total time for transaction =” + TimeElapsed · “success” + transactionID “time” + TimeElapsed · Integration issues can be the hardest to debug; our suggestion is that you log every event that comes in/out of your system to an external system, whether it is HTTP headers, authentications, keep alive, etc.
🌐
GeeksforGeeks
geeksforgeeks.org › java › logger-infostring-method-in-java-with-examples
Logger info(String) method in Java with Examples - GeeksforGeeks
January 11, 2022 - // Java program to demonstrate // Logger.info(String msg) method import java.util.logging.Logger; public class GFG { public static void main(String[] args) { // Create a Logger Logger logger = Logger.getLogger( GFG.class.getName()); // Call info method logger.info("This is message 1"); logger.info("This is message 2"); logger.info("This is message 3"); logger.info("This is message 4"); } } The output printed on eclipse ide is shown below.
🌐
Medium
naveen-metta.medium.com › system-out-println-vs-loggers-in-java-a-comprehensive-guide-ad6df493b737
System.out.println vs Loggers in Java: A Comprehensive Guide | by Naveen Metta | Medium
May 16, 2024 - Two common approaches are using System.out.println and utilizing logging frameworks like Log4j, SLF4J, or java.util.logging. This article explores these two methods, comparing their use cases, advantages, and disadvantages with detailed explanations and ample code examples. System.out.println is a method provided by the PrintStream class in Java, used to print messages to the console.
🌐
Sentry
blog.sentry.io › a-guide-to-logging-and-debugging-in-java
Logging & Debugging in Java - Guide | Sentry Blog
September 25, 2025 - We’ll also look at how Sentry can help you monitor and manage errors in your Java applications. By understanding the fundamentals and advanced techniques, you’ll be equipped to tackle issues confidently within your Java projects. You may have used System.out.println() to log and debug your code, for example:
🌐
Silicon Cloud
silicloud.com › home › how can we make the logs print to the console?
How can we make the logs print to the console? - Blog - Silicon Cloud
March 21, 2024 - In Java, you can print logs to the console using the following code: the logging utility in Java import java.util.logging.*; public class Main { private static final Logger logger = Logger.getLogger(Main.class.getName()); public static void main(String[] args) { ConsoleHandler consoleHandler = new ConsoleHandler(); consoleHandler.setLevel(Level.ALL); logger.addHandler(consoleHandler); logger.info("This is an info message"); logger.warning("This is a warning message"); logger.severe("This […]
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › lang › system › out
Logging System.out.println Results in a Log File Example - Java Code Geeks
August 9, 2017 - It all depends on the scenario we use it. Whatever may be the case, do not use System.out.println for logging to stdout. Log4j is a simple, flexible, and fast Java based logging framework. It is Thread safe and supports internationalization.