log4j JDBCAppender 轮换表名称

发布于 2024-10-05 01:15:06 字数 551 浏览 0 评论 0原文

我已经成功配置了一个应用程序,该应用程序使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剩余の解释 2024-10-12 01:15:06

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

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 就会扩展它们并登录到相应的表中。

但是,它不会在新的月份开始时创建新表,因此目前我必须提前手动创建表,这很不理想。

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.

心不设防 2024-10-12 01:15:06

您必须编写自己的附加程序才能执行此操作。

另一种选择是保留现有的附加程序并执行以下操作:

您的数据库中有一个名为 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 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.

世界如花海般美丽 2024-10-12 01:15:06

每月运行一个脚本并将该特定表转储到备份文件中,然后将其压缩以进行存档怎么样?完成后,截断表或删除日期范围内的行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文