j256 / ormlite-core

Core ORMLite functionality that provides a lite Java ORM in conjunction with ormlite-jdbc or ormlite-android
http://ormlite.com/
ISC License
578 stars 213 forks source link

Setting java logging does not work #280

Closed stefanofornari closed 1 year ago

stefanofornari commented 1 year ago

Hello, I want to use the native java logging, therefore I have set the system property 'com.j256.simplelogger.backend to JavaUtilLogBackend$JavaUtilLogBackendFactory as per the code.

However I get the following error:

LoggerFactory Could not find valid log-type from system property 'com.j256.simplelogger.backend', value 'JavaUtilLogBackend$JavaUtilLogBackendFactory'

Any help is very appreciated.

j256 commented 1 year ago

Wow. The doc suck here. Try: the value "JAVA_UTIL" for the com.j256.simplelogger.backend system property. Also, the code should take a backend as opposed to just a LogBackendType. I'll fix both of those problems.

Let me know if it works.

stefanofornari commented 1 year ago

Hi, thanks for your help. It actually half works. Using JAVA_UTIL makes it to use java logging and I can now configure it through logging.properties. However I see only the CREATE statment logs. Here a simple program to reproduce it:

LoggingDemo

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class LoggingDemo {
    public static final String JDBC_DRIVER_CLASS = "org.hsqldb.jdbc.JDBCDriver";
    public static final String JDBC_CONNECTION_STRING = "jdbc:hsqldb:mem:testdb";

    @DatabaseField(id = true, width = 64)
    public final String id;
    @DatabaseField(canBeNull = false, width=40)
    public final String first;
    @DatabaseField(canBeNull = false, width=40)
    public final String last;

    public LoggingDemo() {
        id = "one";
        first = "first name";
        last = "last name";
    }

    public static void main(String... args) throws Exception {
        Class.forName(JDBC_DRIVER_CLASS);
        ConnectionSource db = new JdbcConnectionSource(JDBC_CONNECTION_STRING);
        Dao<LoggingDemo, String> dao = DaoManager.createDao(db, LoggingDemo.class);

        TableUtils.dropTable(dao, true);
        TableUtils.createTable(dao);

        dao.create(new LoggingDemo());

        db.close();
    }

}

logging.properties

.level = ALL                                                                                                                                                                             
handlers = java.util.logging.ConsoleHandler                                                                                                                                              

com.j256.ormlite.level = ALL                                                                                                                                                             
com.j256.ormlite.dao.level = ALL                                                                                                                                                         
com.j256.ormlite.jdbc.level = ALL                                                                                                                                                        
com.j256.ormlite.stmt.level = ALL                                                                                                                                                        
com.j256.ormlite.table.level = ALL                                                                                                                                                       

run.sh

#!/bin/sh

export CLASSPATH=.:EasyWallet-0.0-SNAPSHOT.jar:hsqldb-2.7.1.jar:ormlite-jdbc-6.1.jar

java -Djava.util.logging.config.file=./logging.properties -Dcom.j256.simplelogger.backend=JAVA_UTIL LoggingDemo

output

./run.sh 
Jan 24, 2023 8:10:17 AM com.j256.ormlite.logger.JavaUtilLogBackend log
INFO: dropping table 'LOGGINGDEMO'
Jan 24, 2023 8:10:18 AM com.j256.ormlite.logger.JavaUtilLogBackend log
INFO: ignoring drop error 'java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: LOGGINGDEMO in statement [DROP TABLE "LOGGINGDEMO" ]' for statement: DROP TABLE "LOGGINGDEMO" 
Jan 24, 2023 8:10:18 AM com.j256.ormlite.logger.JavaUtilLogBackend log
INFO: creating table 'LOGGINGDEMO'
Jan 24, 2023 8:10:18 AM com.j256.ormlite.logger.JavaUtilLogBackend log
INFO: executed create table statement changed 0 rows: CREATE TABLE "LOGGINGDEMO" ("id" VARCHAR(64) , "first" VARCHAR(40) NOT NULL , "last" VARCHAR(40) NOT NULL , PRIMARY KEY ("id") ) 
stefanofornari commented 1 year ago

UPDATE: it works. I needed to add to the configuration the line:

java.util.logging.ConsoleHandler.level = ALL

j256 commented 1 year ago

Great!