dongjun lee
dongjun lee

Reputation: 13

log4j2 without catching or exception

I am beginner of java

I am making some program with excel(poi)

and I've been trying to use log4j2 to log what is wrong when run jar or exe

so I got a question

when I searched how to use log4j2 on internet

there is only a usage which like

try{some method}

catch(exception ex)
     {logger.catching(ex)}

is it the only way to log ?

is there a way to log without using try catch?

for now ,I think if I use try and catch I need to use a lot of try catch or throws..

thank you in advance!

Upvotes: 1

Views: 844

Answers (2)

D.B.
D.B.

Reputation: 4713

Yes, you can log things other than exceptions. In fact you can log anything you want. Please see the log4j2 manual, in particular the page called Java API

You simply create your logger and invoke one of the methods specific to the level you want for your event, or if you're using a custom level use the log method. See the architecture page of the manual for more on log levels.

The code below is from the Java API page of the manual and shows you how to log the message "Hello, World!" at INFO level.

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

public class HelloWorld {
    private static final Logger logger = LogManager.getLogger("HelloWorld");
    public static void main(String[] args) {
        logger.info("Hello, World!");
    }
}

Upvotes: 0

Russiancold
Russiancold

Reputation: 1311

Sure. You can invoke logger.whatever() anywhere. E.g. logger.info(); Method catching() is used to log an exception or error that has been caught. That's why in your example it's used with try-catch block. Read more in docs.

Upvotes: 2

Related Questions