Junior — Middle
What methods are available for implementing a logging system in an application?
sobes.tech AI
Answer from AI
Logging in Java applications can be done using several common methods:
- Using standard logging libraries such as Log4j, Logback, or java.util.logging.
- Configuring logging levels (ERROR, WARN, INFO, DEBUG) to control the amount of output.
- Logging to files, console, or remote systems (e.g., Syslog, ELK Stack).
- Asynchronous logging to minimize performance impact.
- Formatting logs in structured formats (JSON) for easier analysis.
Example with Logback:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Example {
private static final Logger logger = LoggerFactory.getLogger(Example.class);
public void doWork() {
logger.info("Starting work");
try {
// code
} catch (Exception e) {
logger.error("Error during execution", e);
}
}
}