4

How can I fix the error?

log4j.properties

    # Define the root logger with appender file
log4j.rootLogger = ALL, DB

# Define the DB appender
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender

# Set JDBC URL
log4j.appender.DB.URL=jdbc:mysql://localhost/youtube

# Set Database Driver
log4j.appender.DB.driver=com.mysql.jdbc.Driver

# Set database user name and password
log4j.appender.DB.user=root
log4j.appender.DB.password=root

# Set the SQL statement to be executed.
log4j.appender.DB.sql=INSERT INTO logs VALUES('%x','%d','%C','%p','%m')

# Define the layout for file appender
log4j.appender.DB.layout=org.apache.log4j.PatternLayout

jdbcAppender.java

public class jdbcAppender{
  /* Get actual class name to be printed on */
  static Logger log = Logger.getLogger(jdbcAppender.class.getName());

  public static void main(String[] args) throws IOException,SQLException{

     log.debug("Debug");
     log.info("Info");
  }
}

Database

enter image description here

Error

enter image description here

4 Answers 4

3

Depending on the MySQL version that you're using, you need to define the DATED field so it can accept a DATETIME value that contains fractions of a second. By default, DATETIME accepts values as YY-MM-DDDD HH:MM:SS.

Read about it here: http://dev.mysql.com/doc/refman/5.6/en/date-and-time-literals.html#date-and-time-standard-sql-literals

Another problem you may run into is the fact that MySQL expects the fraction's delimiter to be a dot (.), rather than a comma. Your exception's stack trace shows a comma as the delimiter, which I believe is derived from your system's locale.

0
2

You can change the insert statement to change the date string into an appropriate datetime something like:

log4j.appender.DB.sql=INSERT INTO logs VALUES('%x',STR_TO_DATE( '%d', '%Y-%M-%d %H:%i' ),'%C','%p','%m')

This is not the appropriate format, but just an idea of another approach.

I'm just looking into mysql millisecond handling, but I see the date string is: '2012-11-17 16:07:29,995', Is the 995 part milliseconds, or is the 29,995 part a decimal second?

2
  • Another way to try is, override the JDBCAppender, and override the execute method: class MyJDBCAppender extends JDBCAppender { protected void execute(String sql) { super.execute(modifySqlStringToConformToMySql(sql)); }} and use that appender in your log4j.properties file Nov 17, 2012 at 22:46
  • I dont know how critical it is, but you can also go with the solution of configuring a default value for the column DATED to be the current date: stackoverflow.com/questions/168736/… and define the insert statement without the date: log4j.appender.DB.sql=INSERT INTO logs (USER_ID, LOGGER, LEVEL, MESSAGE) VALUES('%x','%C','%p','%m') Nov 17, 2012 at 23:10
1
log4j.appender.DB.sql=INSERT INTO logs VALUES('%x','%d','%C','%p','%m')

into

log4j.appender.DB.sql=INSERT INTO logs VALUES('%x','%d{yyyy-MM-dd HH:mm:ss}','%C','%p','%m')
0

You can change the insert statement to convert the date string into an appropriate format. Something like:

log4j.appender.DB.sql=INSERT INTO logs VALUES('%x','%d{yyyy-MM-DD HH:MM:SS}','%C','%p','%m')

Hope it helps.

1
  • Date format is not correct. MM = month, SS = milliseconds, så the time will be displayed incorrect.
    – homaxto
    Jan 9, 2017 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.