Basil Bourque
Basil Bourque

Reputation: 338316

What is the simplest quick-and-dirty way to start logging Tomcat?

In Apache Tomcat, what is the easiest quick-and-dirty way to do some logging in a fresh project?

The documentation, such as FAQ/Logging and Logging in Tomcat, are overwhelming. I just want to spit out some text to see if my new project is working.

Yes, I know eventually I will need to add SLF4J and Logback (etc.), but what can I do in the meantime?

I'm using NetBeans 8 Beta. So, it would be convenient to see my logging output from inside NetBeans. But at this point I'd settle for going into the file system to open a text file manually.

Upvotes: 0

Views: 217

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 338316

Found the solution in this answer in another question. I'll provide more detail here.

In one line… ( replace YourClassNameGoeshere & YourMessageGoesHere )

java.util.logging.Logger.getLogger( YourClassNameGoeshere.class.getName() ).info( "YourMessageGoesHere" );

In NetBeans 8 Beta, the log entry appears on screen on the Output tab, on the nested tab named for the web server such as Apache Tomcat 8.0.3 Beta.

enter image description here

The entry will look something like this:

01-Mar-2014 22:33:33.957 INFO [http-nio-8080-exec-2] BackgroundTaskControl.contextInitialized Basil says 'hello world'.

Upvotes: 0

JBT
JBT

Reputation: 8746

I'd like to add my two cents here.

Just to start with logging without any configuration hassle, here is what I usually do.

import java.util.logging.Logger;

public class LoggingDemo {
    //First, you instantiate a logger object as a member
    private static final Logger logger = Logger.getLogger( LoggingDemo.class.getName() );

    public Object someMethod( Object someParameter ) {
        // Then, use the logger wherever you want
        logger.info("some message");
        ...
    }
}

Upvotes: 1

Related Questions