log4j JDBCAppender 轮换表名称
我已经成功配置了一个应用程序,该应用程序使用 log4j 进行日志记录以登录到 MySQL 数据库。 (使用 org.apache.log4j.jdbc.JDBCAppender)。
我还有一些 Perl 应用程序也可以登录数据库。我的 Perl 应用程序已设置为数据库表的名称每月都会更改(log_2010_11、log_2010_10 等)。在每个月底,我都会运行刚刚完成的月份的报告脚本,将表转储到外部文件(该文件会被压缩和存档),然后删除该表。这样,日志数据库的总大小保持在合理的范围内。
我想对 log4j 做同样的事情,但似乎没有适合该目的的 log4j 附加程序。
是否可以做这样的事情:
log4j.appender.SQ=org.apache.log4j.jdbc.JDBCRollingAppender
log4j.appender.SQ.Driver=com.mysql.jdbc.Driver
log4j.appender.SQ.URL=jdbc:mysql://localhost:3306/logs_{%year}_{%month}
谢谢。
I have successfully configured an application that uses log4j for it's logging to log into a MySQL database. (Using org.apache.log4j.jdbc.JDBCAppender).
I also have some perl applications that log into the database as well. My perl apps are setup so that the name of the database table changes every month (log_2010_11, log_2010_10 etc). At the end of each month, I run reporting scripts on the month just completed, dump the table to an external file (which gets compressed and archived), and then drop the table. This way the total size of the logging database stays within sensible limits.
I would like to do the same with log4j, but there does not appear to be a log4j appender suitable for the purpose.
Is it possible to do something like this:
log4j.appender.SQ=org.apache.log4j.jdbc.JDBCRollingAppender
log4j.appender.SQ.Driver=com.mysql.jdbc.Driver
log4j.appender.SQ.URL=jdbc:mysql://localhost:3306/logs_{%year}_{%month}
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想出了如何做到这一点:
看来您只需将日期格式字符串放入 SQL 语句中,JDBCAppender 就会扩展它们并登录到相应的表中。
但是,它不会在新的月份开始时创建新表,因此目前我必须提前手动创建表,这很不理想。
I figured out how to do this:
It appears you can just put date format strings into the SQL statement, and JDBCAppender will expand them and log into the coresponding table.
However, it will not create new tables at the start of the new month, so currently I have to manualy create the tables beforehand, which is far from ideal.
您必须编写自己的附加程序才能执行此操作。
另一种选择是保留现有的附加程序并执行以下操作:
您的数据库中有一个名为
log
的表。为什么不制作一个 Perl 脚本,在每个月末创建一个新表,假设 12 月为log_12
,将所有内容从log
复制到log_12
然后删除log
中的所有内容?这样你就不必费力去制作另一个附加程序。You'd have to write your own appender to do this.
Another option would be to stay with the existing appender and do this:
You have a table in your database named
log
. Why not make a Perl script that makes a new table at end of every month, let's saylog_12
for December, copies everything fromlog
tolog_12
and then delete everything fromlog
? That way you don't have to mess around with making another appender.每月运行一个脚本并将该特定表转储到备份文件中,然后将其压缩以进行存档怎么样?完成后,截断表或删除日期范围内的行。
How about a script to run monthly and dump that particular table into a back up file and then zip it for archiving. Upon complete, truncate the table or delete rows within date range.