Link to home
Start Free TrialLog in
Avatar of geek4u
geek4u

asked on

Log4j Database related

I have an existing code which used log4j to log to a file. I need to log to Databases. I want basic guidance of what to do and how to do?

What needs to be changed without changing the source code so that the statements are logged into database.

Please help.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of geek4u
geek4u

ASKER

I dont want to change the source code but i need to externally send the log to Databases. The above links dont explain how to do it.

Please suggest
Avatar of geek4u

ASKER

please can you be more clear!! Just explain the steps that need to be done.

Thanks,
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You need to set up the db to take the fields you want to log and then configure log4j.properties. I would follow

# JDBC appender directly, 2b)

as per my last link
You have add an appender to log category for this and add the insert query for this appender in the config file.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of geek4u

ASKER

My log4j.properties looks like this:

# Attach appender A1 to root. Set root level to Level.DEBUG.
log4j.rootLogger=DEBUG, A1, A1X, DB

# A1 is set to be a FileAppender sending its output to file
# However, only warning messages and above will be printed
# in A1 because A1's threshold is set to Level.WARN.

# The fact that the root level is set to Prority.DEBUG only influences
# log requests made to the root logger. It has no influence on the
# *appenders* attached to root.

# Valid Log levels DEBUG, INFO, WARN, ERROR
log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.Threshold=WARN
# log4j.appender.A1.File=/usr/kasenna/tomcat/logs/osf.log
log4j.appender.A1.File=/usr/kasenna/LivingRoom/tomcat/logs/osf.log

# Set File Size and How many backup copies to keep
log4j.appender.A1.MaxFileSize=5MB
log4j.appender.A1.MaxBackupIndex=10
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %p [%t] %c{2} (%M:%L) - %m%n

log4j.appender.A1X=org.apache.log4j.CompositeRollingAppender
log4j.appender.A1X.RollingStyle=3
log4j.appender.A1X.Threshold=WARN
log4j.appender.A1X.File=/usr/kasenna/LivingRoom/tomcat/logs/osfx.log4j
log4j.appender.A1X.datePattern='.'yyyy-MM-dd
log4j.appender.A1X.dateExpireInterval=5 Days
log4j.appender.A1X.MaxSizeRollBackups=2
log4j.appender.A1X.layout=org.apache.log4j.xml.XMLLayout

log4j.appender.DB=org.apache.log4j.jdbcplus.JDBCAppender
log4j.appender.DB.Threshold=WARN
log4j.appender.DB.dbclass=com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource
log4j.appender.DB.url=jdbc:mysql://172.31.254.103:3306/osfdb
log4j.appender.DB.username=IPTVuser
log4j.appender.DB.password=1ptvus3r
log4j.appender.DB.layout=org.apache.log4j.PatternLayout
log4j.appender.DB.layout.ConversionPattern=%d %p [%t] %c{2} (%M:%L) - %m%n
log4j.appender.DB.sql=INSERT INTO osfLog (id, prio, cat, thread, msg, layout_msg, throwable, ndc, mdc, mdc2, info, addon, created_by) VALUES (@INC@, '@PRIO@', '@CAT@', '@THREAD@', '@MSG@', '@LAYOUT:1@', '@THROWABLE@', '@NDC@', '@MDC:MyMDC@', '@MDC:MyMDC2@', '@TIMESTAMP@', '@LAYOUT@','log4j')

# Set the Audit Log Appender
log4j.category.com.kasenna.osf.core.AuditLog=INFO, AL, ADB
#ALX
log4j.appender.AL=org.apache.log4j.RollingFileAppender
log4j.appender.AL.File=/usr/kasenna/LivingRoom/osf/logs/audit.log

log4j.appender.AL.MaxFileSize=5MB
log4j.appender.AL.MaxBackupIndex=10
log4j.appender.AL.layout=com.kasenna.osf.core.AuditLogPatternLayout
log4j.appender.AL.layout.ConversionPattern="%d{ISO8601}" %m%n

#log4j.appender.ALX=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.ALX.File=/usr/kasenna/LivingRoom/osf/logs/audit.log4j
#log4j.appender.ALX.DatePattern=yyyyMMdd-HH'.log4j'
#log4j.appender.ALX.layout=org.apache.log4j.xml.XMLLayout


log4j.appender.ADB=org.apache.log4j.jdbcplus.JDBCAppender
log4j.appender.ADB.dbclass=com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource
log4j.appender.ADB.url=jdbc:mysql://172.31.254.103:3306/osfdb
log4j.appender.ADB.username=IPTVuser
log4j.appender.ADB.password=1ptvus3r
log4j.appender.ADB.sql=INSERT INTO auditLog (id, prio, cat, thread, msg, layout_msg, throwable, ndc, mdc, mdc2, info, addon,created_by) VALUES (@INC@, '@PRIO@', '@CAT@', '@THREAD@', '@MSG@', '@LAYOUT:1@', '@THROWABLE@', '@NDC@', '@MDC:MyMDC@', '@MDC:MyMDC2@', '@TIMESTAMP@', '@LAYOUT@','log4j')
log4j.appender.ADB.layout=com.kasenna.osf.core.AuditLogPatternLayout
log4j.appender.ADB.layout.ConversionPattern="%d{ISO8601}" %m%n

# Set the SNMP Log Appender
log4j.category.com.kasenna.osf.core.SNMPLog=INFO, TRAP
#,SNMPDB

log4j.appender.TRAP=org.apache.log4j.RollingFileAppender
log4j.appender.TRAP.File=/usr/kasenna/LivingRoom/osf/logs/SNMPTrap.log

log4j.appender.TRAP.MaxFileSize=5MB
log4j.appender.TRAP.MaxBackupIndex=10
log4j.appender.TRAP.layout=org.apache.log4j.PatternLayout
log4j.appender.TRAP.layout.ConversionPattern=%d %p [%t] %c{2} (%M:%L) - %m%n


# Control logging for Jakarta packages classes
log4j.logger.org.apache.commons=ERROR
log4j.logger.org.apache.struts=WARN

# Control logging for Tomcat packages classes
# Comment the following line to activate looging for catalina classes
log4j.logger.org.apache.catalina=ERROR


#log4j.appender.SNMPDB=org.apache.log4j.jdbcplus.JDBCAppender
#log4j.appender.SNMPDB.dbclass=com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource
#log4j.appender.SNMPDB.url=jdbc:mysql://localhost:3306/osfdb
#log4j.appender.SNMPDB.username=posuser
#log4j.appender.SNMPDB.password=pos-user
#log4j.appender.SNMPDB.sql=INSERT INTO snmpLog (id, prio, cat, thread, msg, layout_msg, throwable, ndc, mdc, mdc2, info, addon,created_by) VALUES (@INC@, '@PRIO@', '@CAT@', '@THREAD@', '@MSG@', '@LAYOUT:1@', '@THROWABLE@', '@NDC@', '@MDC:MyMDC@', '@MDC:MyMDC2@', '@TIMESTAMP@', '@LAYOUT@','log4j')
#log4j.appender.SNMPDB.layout=org.apache.log4j.PatternLayout
#log4j.appender.SNMPDB.layout.ConversionPattern=%d %p [%t] %c{2} (%M:%L) - %m%n


My Database table is:

mysql> desc auditLog;
+------------+--------------+------+-----+-------------------+-------+
| Field      | Type         | Null | Key | Default           | Extra |
+------------+--------------+------+-----+-------------------+-------+
| id         | varchar(255) | YES  |     | NULL              |       |
| prio       | varchar(10)  | YES  |     | NULL              |       |
| cat        | varchar(255) | YES  |     | NULL              |       |
| thread     | varchar(64)  | YES  |     | NULL              |       |
| msg        | varchar(255) | YES  |     | NULL              |       |
| layout_msg | varchar(255) | YES  |     | NULL              |       |
| throwable  | varchar(255) | YES  |     | NULL              |       |
| ndc        | varchar(10)  | YES  |     | NULL              |       |
| mdc        | varchar(10)  | YES  |     | NULL              |       |
| mdc2       | varchar(10)  | YES  |     | NULL              |       |
| info       | varchar(255) | YES  |     | NULL              |       |
| addon      | varchar(255) | YES  |     | NULL              |       |
| created_by | varchar(64)  | YES  |     | NULL              |       |
| time       | timestamp    | YES  |     | CURRENT_TIMESTAMP |       |
+------------+--------------+------+-----+-------------------+-------+
14 rows in set (0.00 sec)


Its not working. When i do some activity it gets logged into audit.log file but it does not get logged in the Database. Please look at the configuration whether it is correct.

Urgent response awaited.

Thanks in advance.
Either the URL could be wrong. Or the driver class might not be in the classpath. Or it could be that you are logging only debug/ info messages - you have set the JDBC logger to log only warn and more severe messages.
Avatar of geek4u

ASKER

The URL is correct. The driver is in the class path as well. I changed the WARN to DEBUG. Please Advice what to do? Its still not working
Try to use
log4j.appender.db.Driver="full qualified driver classname" instead of
log4j.appender.DB.dbclass=com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource
Avatar of geek4u

ASKER

I added this line instead of the line mentioned above:

log4j.appender.db.Driver=com.mysql.jdbc.Driver

It still does not work. Please anyone suggest a way out.

Thanks
try use:
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender
instead of
log4j.appender.DB=org.apache.log4j.jdbcplus.JDBCAppender
Avatar of geek4u

ASKER

That does not work either.

This is how the changes look like:

log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DB.Threshold=DEBUG
#log4j.appender.DB.dbclass=com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource
log4j.appender.DB.dbclass=com.mysql.jdbc.Driver
log4j.appender.DB.url=jdbc:mysql://172.31.254.103:3306/osfdb
log4j.appender.DB.username=IPTVuser
log4j.appender.DB.password=1ptvus3r
log4j.appender.DB.layout=org.apache.log4j.PatternLayout
log4j.appender.DB.layout.ConversionPattern=%d %p [%t] %c{2} (%M:%L) - %m%n
log4j.appender.DB.sql=INSERT INTO osfLog (id, prio, cat, thread, msg, layout_msg, throwable, ndc, mdc, mdc2, info, addon, created_by) VALUES (@INC@, '@PRIO@', '@CAT@', '@THREAD@', '@MSG@', '@LAYOUT:1@', '@THROWABLE@', '@NDC@', '@MDC:MyMDC@', '@MDC:MyMDC2@', '@TIMESTAMP@', '@LAYOUT@','log4j')

I tried DB.Driver as well before changing to DB.dbclass.

Please look into it.

Thanks
Strange! I have the same configuration (but with oracle) and they are working.

Why you don't try a simple table (say INSERT INTO Test (LoggingDate) VALUES ('%d'))

and without MDC and NDC.

Try create a small table and try to insert some values and see if it works with the same configuration.
Avatar of geek4u

ASKER

Please provide a sample. It will help me a lot.

Thanks, I appreciate it. I have been stuck in this problem for 4 days now.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial