log4j JDBCAppender旋转表的名称[英] log4j JDBCAppender rotate table name

本文是小编为大家收集整理的关于log4j JDBCAppender旋转表的名称的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我已经成功配置了一个使用log4j的应用程序,以登录登录MySQL数据库. (使用org.apache.log4j.jdbc.jdbcappender).

我也有一些登录数据库的PERL应用程序.我的perl应用程序已设置,因此数据库表的名称每个月都会更改(log_2010_11,log_2010_10等).在每个月结束时,我在刚刚完成的月份运行报告脚本,将表倒入外部文件(被压缩和存档),然后丢弃表.这样,记录数据库的总大小停留在明智的范围内.

我想对log4j做同样的事情,但是似乎没有适合该目的的log4j appender.

可以做这样的事情:

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}

谢谢.

推荐答案

我想出了如何做到这一点:

log4j.appender.SQ=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.SQ.Driver=com.mysql.jdbc.Driver
log4j.appender.SQ.URL=jdbc:mysql://localhost:3306/logs
log4j.appender.SQ.sql=INSERT INTO accesslog_%d{yyyy_MM} (date, time, tz, ...

看来,您可以将日期格式字符串放入SQL语句中,而JDBCappender将将它们展开并登录到corespending表中.

但是,它不会在新月开始时创建新桌子,因此目前我必须事先手动创建桌子,这远非理想.

其他推荐答案

您必须编写自己的应用程序才能做到这一点.

另一个选择是与现有的appender保持联系,并执行此操作:

您的数据库中有一个名为log的表.为什么不制作一个在每个月结束时制作新表格的perl脚本,假设12月的log_12,将所有内容从log> log_12中复制,然后从log中删除所有内容?这样,您就不必弄乱做另一个appender.

其他推荐答案

脚本每月运行并将该特定表倒入备份文件,然后将其缩回以进行归档.完成后,在日期范围内截断表或删除行.

本文地址:https://www.itbaoku.cn/post/1574845.html

问题描述

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.

推荐答案

I figured out how to do this:

log4j.appender.SQ=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.SQ.Driver=com.mysql.jdbc.Driver
log4j.appender.SQ.URL=jdbc:mysql://localhost:3306/logs
log4j.appender.SQ.sql=INSERT INTO accesslog_%d{yyyy_MM} (date, time, tz, ...

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.

其他推荐答案

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 say log_12 for December, copies everything from log to log_12 and then delete everything from log? 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.