百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

Linux 使用 logrotate 日志归档_linux日志管理

off999 2025-02-16 22:25 11 浏览 0 评论

系统时时刻刻都在产生日志,如果不及时清理,很快就会灌满硬盘,但如果要手工清理,又很麻烦。

这种情况下,logrotate 这个程序很好的完成这个任务。

logrotate 用来把旧的日志文件删除,并创建新的日志文件,我们把它叫做“转储”。

我们可以根据日志文件的大小,也可以根据其天数来转储,这个过程一般通过一个叫做crond的守护进程来执行,logrotate 还可以用于压缩日志文件,以及发送日志到指定的E-mail 。

logrotate 的配置文件是 /etc/logrotate.conf。

查看当前版本

在系统中使用命令来查看版本:

logrotate --version

一些使用的方法,请参考下面的内容:

查看使用手册

man logrotate

nginx 日志归档

在默认情况下,操作系统已经为我们配置了 nginx 的默认日志归档。

归档的配置文件为: /etc/logrotate.d/nginx

可以直接编辑这个文件。

/var/log/nginx/*.log
/var/log/nginx/src.isharkfly.com/*.log
{
        daily
        missingok
        rotate 14
        compress
        delaycompress
        notifempty
        create 0640 www-data adm
        sharedscripts
        prerotate
                if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                        run-parts /etc/logrotate.d/httpd-prerotate; \
                fi \
        endscript
        postrotate
                invoke-rc.d nginx rotate >/dev/null 2>&1
        endscript
}

对我们来说,我们虚拟主机的日志和 nginx 的日志在相同的目录下,只是在下面添加了一个文件夹。

所以,我们就在上面添加文件夹就行。

归档测试

配置文件完成修改后,可以对配置进行测试:

运行命令:

logrotate /etc/logrotate.d/nginx --debug

服务器上输出的内容为:

root@ns564012:/etc/logrotate.d# logrotate /etc/logrotate.d/nginx --debug
warning: logrotate in debug mode does nothing except printing debug messages!  Consider using verbose mode (-v) instead if this is not what you want.

reading config file /etc/logrotate.d/nginx
Reading state from file: /var/lib/logrotate/status
Allocating hash table for state file, size 64 entries
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state

Handling 1 logs

rotating pattern: /var/log/nginx/*.log
/var/log/nginx/src.isharkfly.com/*.log
 after 1 days (14 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
  Now: 2025-02-06 02:38
  Last rotated at 2025-02-06 00:00
  log does not need rotating (log has been rotated at 2025-02-06 00:00, which is less than a day ago)
considering log /var/log/nginx/error.log
  Now: 2025-02-06 02:38
  Last rotated at 2025-02-06 00:00
  log does not need rotating (log has been rotated at 2025-02-06 00:00, which is less than a day ago)
considering log /var/log/nginx/src.isharkfly.com/access.log
Creating new state
  Now: 2025-02-06 02:38
  Last rotated at 2025-02-06 02:00
  log does not need rotating (log has already been rotated)
considering log /var/log/nginx/src.isharkfly.com/error.log
Creating new state
  Now: 2025-02-06 02:38
  Last rotated at 2025-02-06 02:00
  log does not need rotating (log has already been rotated)
not running prerotate script, since no logs will be rotated
not running postrotate script, s

对我们来说,只需要日志进行归档就行,因为我们不希望日志内容占用过多的磁盘空间。

相关推荐

Python进阶教程:如何自定义异常(附电商库存管理案例)

在Python中,你可以使用异常(exceptions)来预期和处理那些干扰程序正常执行流程的错误。Python内置了许多常见的异常,例如ValueError、TypeError和KeyError,但...

Python 中使用try来处理异常的方法

六二,直方大,不习无不利。在学习python中会经常遇到各种异常事件;现归纳一下如何捕捉并处理这些异常;今天来给大家整理一下。一、异常的概念什么是异常?异常即是一个事件,该事件会在程序执行过程中发生,...

python入门-day3:异常处理(异常处理方法及流程python)

异常处理的内容,包括try、except、finally的用法,介绍常见异常类型,最后通过一个练习(处理用户输入的非法数字)帮你把知识点串起来。我会用简单易懂的语言,确保新手也能轻松掌握。Da...

16-Python的异常(python的异常类型及处理)

1-异常介绍1-2-什么是异常异常是程序运行时发生的错误或异常情况,它会中断正常的程序执行流程;Python使用异常处理机制来处理这些运行时错误。1-4-为什么要捕获异常异常会中断程序的执行;1-3-...

Python 异常处理详解(python中异常)

一、什么是异常?核心定义在程序运行过程中,当Python解释器检测到无法继续执行的操作时,会立即创建异常对象并中断当前流程。此时若不处理异常,程序将直接崩溃退出。典型场景与原理用户输入无效数据nu...

掌握这些 Python 异常处理技巧,代码稳如老狗!

在Python中,异常处理不仅可以帮助我们捕获和处理错误,还能让我们更清晰地了解错误发生的背景。1.异常层次结构Python内置了许多异常,我们在编程时可能会经常遇到,例如ZeroDivisi...

python异常处理机制最佳实践(python异常处理总结)

Python异常处理的最佳实践需要兼顾代码健壮性、可读性和性能。以下是经过工程验证的10个核心原则和技巧:一、精准捕获原则避免裸except禁止使用except:或exceptExce...

python之异常处理(python异常处理过程可以概括为三个步骤)

异常语法try:<代码块>except<异常类型>:<代码块>捕获通用异常try:<代码块>exceptExceptionas...

一天学一点,今天学习掌握Python:异常处理与文件操作全攻略

这一笔记记录了我学习python的异常和文件的操作,这也是针对Python异常和文件操作教程的进一步优化建议和注意事项:异常处理优化1.避免过度捕获异常o不要为了捕获异常而捕获异常,应根据实际需求...

新手学Python避坑,学习效率狂飙! 十二、Python 异常处理

异常处理系统分享在Python里,异常指的是程序运行期间出现的错误。当异常发生时,正常的程序流程会被中断,Python会抛出异常对象。为了防止程序因异常而崩溃,你可以使用异常处理机制捕获并处理这...

Python异常处理全攻略:从入门到精通,轻松应对代码“翻车”时刻

喜欢的条友记得关注、点赞、转发、收藏,你们的支持就是我最大的动力源泉。引言:异常处理——代码世界的“保险丝”在编程的世界里,错误和异常就像路上的“坑”,稍不留神就会让你的程序“翻车”。而异常处理,就是...

Python异常处理全面指南(python异常处理步骤)

Python异常处理完全指南异常处理是编写健壮程序的关键技术。Python提供了灵活的语法和丰富的内置异常类型,能够有效管理程序运行时的错误。以下是Python异常处理的全面指南:一、异常处理...

进阶突破python——异常处理机制(异常处理用什么语句python)

Python的异常处理机制是其健壮性设计的核心,通过清晰的语法结构和灵活的处理策略实现错误管理。以下从核心语法、异常对象、高级特性和最佳实践四个维度详解:一、核心语法结构Python使用try-...

Python基础编程——详细介绍Python的异常捕获示例代码

这篇文章主要为大家详细介绍了python的异常捕获,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助①捕捉一个异常捕捉一个异常以用0作为除数会得到Ze...

Python学习 -- 异常捕获技巧(python怎么捕获异常)

在编写Python代码时,异常处理是确保程序稳定性的关键。Python提供了灵活的异常捕获机制,包括try...except语句、try...except...else语句和try...except....

取消回复欢迎 发表评论: