HOWTO · Java

console.log in Java

This article introduces console.log in Java, exploring various methods for logging messages to the console. Learn how to use System.out.println, Log4j, and SLF4J for effective debugging and logging practices in your Java applications. Discover the best practices and enhance your development experience with structured logging techniques.

On this page

Java has no built-in console.log function; that name belongs to JavaScript. For a quick message in a Java program, use System.out.println. For an application that needs levels, configurable destinations, or consistent formatting, use a logging API or framework.

Using System.out.println

System.out.println writes a value followed by a line break to standard output. This complete Java program prints one greeting:

public class ConsoleExample {
    public static void main(String[] args) {
        String message = "Hello, Java!";
        System.out.println(message);
    }
}
Hello, Java!

The program sends the value of message to standard output. Use this approach for a small command-line program, a quick diagnostic, or output intended for the person running the program. It is simple, but it does not provide logging levels, timestamps, or configurable destinations. In a larger application, scattered print statements are harder to filter and manage.

If familiar call syntax is useful, you can write your own wrapper: define a Console class with a static void log(String message) method whose body calls System.out.println(message), then call Console.log("Ready"). Console.log is your code, not a built-in Java API; Java does not gain JavaScript’s console.log this way.

Utilizing Java Logging Frameworks

A logging API lets code record messages by severity, while a logging implementation handles formatting and destinations such as the console or a file. Levels commonly distinguish diagnostic messages from warnings and errors. Configuration can control which messages are shown and where they go, so routine output does not have to be mixed with application logs.

Java includes java.util.logging in the standard library. It is a practical choice when you want a logging API without adding a third-party dependency. Log4j is a separate logging implementation with configurable appenders and layouts. SLF4J is different: it is a facade that gives application code a common logging API, and it needs a compatible provider or binding to produce log output. Choose one implementation for an application unless you have a specific integration reason to bridge or combine logging systems.

Here is a small java.util.logging example: obtain a logger with Logger logger = Logger.getLogger(MyClass.class.getName());, then call logger.info("Service started");, logger.warning("Retrying request");, or logger.severe("Request failed");. These calls use the JDK’s built-in logging setup, so no extra dependency is needed. With the default console handler, a representative line contains a timestamp, the level, and the message, for example INFO: Service started; the full prefix and enabled levels depend on the runtime logging configuration. This is an illustration of the API and its typical format, not a reported execution result.

Implementing SLF4J with Logback

To use SLF4J with Logback in a Maven project, add the org.slf4j:slf4j-api dependency and the ch.qos.logback:logback-classic provider at compatible versions. The latter supplies the Logback implementation for the SLF4J API. Select versions through your project’s dependency management; the versions in older examples may no longer be appropriate, so this guide does not label a fixed pair as current.

In a Java class, declare private static final Logger logger = LoggerFactory.getLogger(MyClass.class); and record messages with calls such as logger.info("Service started"), logger.warn("Retrying request"), or logger.error("Request failed", exception). The last form includes an exception so the backend can record its stack trace. With Logback on the runtime classpath, the provider routes these calls according to its configuration. For example, a console configuration could show INFO MyClass - Service started; this is representative formatting, not an observed result, and the actual threshold, format, and destination depend on Logback configuration. Keeping application calls on SLF4J makes it possible to change the provider with less impact on code that uses the facade.

Conclusion

Use System.out.println when a small program needs straightforward console output. For application logging with severity levels, configuration, and selectable destinations, use a logging implementation such as Log4j or Logback. java.util.logging is available in the Java standard library, while SLF4J provides a facade that connects application code to a provider such as Logback.

FAQ

What is logging used for in Java? Logging records events that help explain what an application is doing, diagnose problems, and monitor its operation. Avoid recording secrets or other sensitive values in log messages.

How do logging levels help? Levels such as debug, info, warn, and error let developers separate diagnostic detail from notable events and failures. A configuration can set a threshold to control which levels are emitted.

Can one application use multiple logging frameworks? It can include multiple logging systems or bridges, but that adds configuration and can produce duplicate output. Prefer one backend and a deliberate bridge when a dependency uses another logging API.

Is SLF4J itself a logging backend? No. SLF4J is a facade. Add a compatible provider, such as Logback, so calls made through the facade are handled by an implementation.

When should I choose a framework over System.out.println? Choose a framework when you need to filter by level, change destinations or formatting through configuration, or manage logs consistently across application components. For a short program or a single visible result, System.out.println is usually enough.