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

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

off999 2025-02-16 22:25 19 浏览 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中面向对象的类属性和实例属性?

类属性和实例属性类属性就是给类对象中定义的属性通常用来记录与这个类相关的特征类属性不会用于记录具体对象的特征类属性的理解:类属性是与类自身相关联的变量,而不是与类的实例关联。它们通...

Java程序员,一周Python入门:面向对象(OOP) 对比学习

Java和Python都是**面向对象编程(OOP)**语言,无非是类、对象、继承、封装、多态。下面我们来一一对比两者的OOP特性。1.类和对象Java和Python都支持面向对象...

松勤技术精选:Python面向对象魔术方法

什么是魔术方法相信大家在使用python的过程中经常会看到一些双下划线开头,双下划线结尾的方法,我们把它统称为魔术方法魔术方法的特征魔术方法都是双下划线开头,双下划线结尾的方法魔术方法都是pytho...

[2]Python面向对象-【3】方法(python3 面向对象)

方法的概念在Python中,方法是与对象相关联的函数。方法可以访问对象的属性,并且可以通过修改对象的属性来改变对象的状态。方法定义在类中,可以被该类的所有对象共享。方法也可以被继承并重载。方法的语法如...

一文带你理解python的面向对象编程(OOP)

面向对象编程(OOP,Object-OrientedProgramming)是一个较难掌握的概念,而Python作为一门面向对象的语言,在学习其OOP特性时,许多人都会对“继承”和“多态”等...

简单学Python——面向对象1(编写一个简单的类)

Python是一种面向对象的编程语言(ObjectOrientedProgramming),在Python中所有的数据类型都是对象。在Python中,也可以自创对象。什么是类呢?类(Class)是...

python进阶突破面向对象——四大支柱

面向对象编程(OOP)有四大基本特性,通常被称为"四大支柱":封装(Encapsulation)、继承(Inheritance)、多态(Polymorphism)和抽象(Abstrac...

Python学不会来打我(51)面向对象编程“封装”思想详解

在面向对象编程(Object-OrientedProgramming,简称OOP)中,“封装(Encapsulation)”是四大核心特性之一(另外三个是继承、多态和抽象),它通过将数据(属性)和...

Python之面向对象:对象属性解析:MRO不够用,补充3个方法

引言在前面的文章中,我们谈及Python在继承关系,尤其是多继承中,一个对象的属性的查找解析顺序。由于当时的语境聚焦于继承关系,所以只是简要提及了属性解析顺序同方法的解析顺序,而方法的解析顺序,在Py...

Python之面向对象:通过property兼顾属性的动态保护与兼容性

引言前面的文章中我们简要提及过关于Python中私有属性的使用与内部“名称混淆”的实现机制,所以,访问私有属性的方法至少有3种做法:1、使用实例对象点操作符的方式,直接访问名称混淆后的真实属性名。2、...

Python之面向对象:私有属性是掩耳盗铃还是恰到好处

引言声明,今天的文章中没有一行Python代码,更多的是对编程语言设计理念的思考。上一篇文章中介绍了关于Python面向对象封装特性的私有属性的相关内容,提到了Python中关于私有属性的实现是通过“...

Python中的私有属性与方法:解锁面向对象编程的秘密

Python中的私有属性与方法:解锁面向对象编程的秘密在Python的广阔世界里,面向对象编程(OOP)是一种强大而灵活的方法论,它帮助我们更好地组织代码、管理状态,并构建可复用的软件组件。而在这个框...

Python 面向对象:掌握类的继承与组合,让你的代码更高效!

引言:构建高效代码的基石Python以其简洁强大的特性,成为众多开发者首选的编程语言。而在Python的面向对象编程(OOP)范畴中,类的继承和组合无疑是两大核心概念。它们不仅能帮助我们实现代码复用,...

python进阶-Day2: 面向对象编程 (OOP)

以下是为Python进阶Day2设计的学习任务,专注于面向对象编程(OOP)的核心概念和高阶特性。代码中包含详细注释,帮助理解每个部分的实现和目的。任务目标:复习OOP基础:类、对象、继...

外婆都能学会的Python教程(二十八):Python面向对象编程(二)

前言Python是一个非常容易上手的编程语言,它的语法简单,而且功能强大,非常适合初学者学习,它的语法规则非常简单,只要按照规则写出代码,Python解释器就可以执行。下面是Python的入门教程介绍...

取消回复欢迎 发表评论: