Solved

Log4j Database related

Posted on 2006-07-21
23
1,280 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:geek4u
  • 7
  • 4
  • 4
  • +3
23 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 150 total points
Comment Utility
0
 
LVL 23

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 125 total points
Comment Utility
try using JDBC appender. You don't have to change the code.

http://logging.apache.org/log4j/docs/api/org/apache/log4j/jdbc/JDBCAppender.html
0
 

Author Comment

by:geek4u
Comment Utility
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
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
0
 

Author Comment

by:geek4u
Comment Utility
please can you be more clear!! Just explain the steps that need to be done.

Thanks,
0
 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 100 total points
Comment Utility
>> I dont want to change the source code but i need to externally send the log to Databases

You don't have to. You can change the configuration from the configuration file (do you have a properties file or an XML file?). You can configure new appenders simply from the configuration-file (like a JDBC appender) and make it subscribe to certain log-types (like say Info and above). What does your config XML/ properties file look like?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
0
 
LVL 23

Expert Comment

by:Ajay-Singh
Comment Utility
You have add an appender to log category for this and add the insert query for this appender in the config file.
0
 
LVL 2

Assisted Solution

by:RoyalNepal
RoyalNepal earned 75 total points
Comment Utility
Hi,
 
 The best way to do is..use JDBCAppender so that you dont have to change the configuration file..

there are lots of examples avialable online..

Thanks,
Roy.

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:geek4u
Comment Utility
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.
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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.
0
 

Author Comment

by:geek4u
Comment Utility
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
0
 

Expert Comment

by:firas981
Comment Utility
Try to use
log4j.appender.db.Driver="full qualified driver classname" instead of
log4j.appender.DB.dbclass=com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource
0
 

Author Comment

by:geek4u
Comment Utility
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
0
 

Expert Comment

by:firas981
Comment Utility
try use:
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender
instead of
log4j.appender.DB=org.apache.log4j.jdbcplus.JDBCAppender
0
 

Author Comment

by:geek4u
Comment Utility
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
0
 

Expert Comment

by:firas981
Comment Utility
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.

0
 
LVL 2

Expert Comment

by:RoyalNepal
Comment Utility
Try create a small table and try to insert some values and see if it works with the same configuration.
0
 

Author Comment

by:geek4u
Comment Utility
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.
0
 

Assisted Solution

by:firas981
firas981 earned 50 total points
Comment Utility
If you have Oracle you may try this (they working for me):

log4j.appender.db=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.db.Driver=oracle.jdbc.driver.OracleDriver
log4j.appender.db.URL=jdbc:oracle:thin:@localhost:1521:your_db_name
log4j.appender.db.user=your_user
log4j.appender.db.password=your_pass
log4j.appender.db.sql=INSERT INTO LOG VALUES ('%d %-5p -%F %m')
log4j.appender.db.layout=org.apache.log4j.PatternLayout
log4j.appender.db.layout.ConversionPattern=%d %-5p - %m
log4j.appender.db.Threshold=debug

where log table has just a msg field.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now