Doma2 uses the JDBCLogger interface for log output, and UtilLoggingJdbcLogger is provided as an example of its implementation. Since this UtilLoggingJdbcLogger adopts JUL (java.util.logging) for log output, the system property java.util.logging.config.file can be used for the settings related to log output.
logging.properties
handlers= java.util.logging.ConsoleHandler, java.util.logging.FileHandler
java.util.logging.FileHandler.level = INFO
java.util.logging.FileHandler.pattern = logging_by_propertiesfile.log
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
For example, if you have the above logging.properties and want to set it to UtilLoggingJdbcLogger, you can do it with -Djava.util.logging.config.file = \ path \ to \ logging.properties. is.
You can also extend UtilLoggingJdbcLogger yourself without using the system properties file.
First, create a class that inherits UtilLoggingJdbcLogger. At this time, set the log output for super.logger (instance of JUL).
MyUtilLoggingJdbcLogger.java
public class MyUtilLoggingJdbcLogger extends UtilLoggingJdbcLogger {
public MyUtilLoggingJdbcLogger() {
try {
Formatter formatter = new SimpleFormatter();
Handler consoleHandler = new ConsoleHandler();
consoleHandler.setFormatter(formatter);
consoleHandler.setLevel(Level.INFO);
Handler fileHandler = new FileHandler("logging_by_class.log");
fileHandler.setFormatter(formatter);
fileHandler.setLevel(Level.INFO);
super.logger.addHandler(consoleHandler);
super.logger.addHandler(fileHandler);
} catch (SecurityException | IOException e) {
e.printStackTrace();
}
}
}
After that, set to use the MyUtilLoggingJdbcLogger created in the Config class of Doma2. Specifically, just override the getJdbcLogger method and return the MyUtilLoggingJdbcLogger instance.
AppConfig.java
@SingletonConfig
public class AppConfig implements Config {
private final JdbcLogger logger;
private AppConfig() {
logger = new MyUtilLoggingJdbcLogger();
}
@Override
public JdbcLogger getJdbcLogger() {
return logger;
}
}
Recommended Posts