Nginx 配置日志以天为单位分开保存

Nginx 配置日志以天为单位分开保存,第1张

服务器上nginx的日志体积日益增大,已经到了打开都要加载很久的地步了,无法忍受,想配置日志按天保首纳存,方便查看。网上搜了一下,基本有两种思路:

主要思路是写一个定时任务脚本,每天特定时间把现在的日志重命名,XXXX2021-01-05.log,然后 nginx -s reopen 重启nginx来实现目的。

考虑了一下nginx重启会导致线上项目服务终止影响用户体验,所以放弃了。

通过nginx.conf配置来实现自动化的按天保存日志,nginx无需重启,好处显而易见,我选择了这种方式实现。

$time_iso8601 是nginx 内嵌变量,可以用来获取当前时间,时间格式如下: 2018-09-21T16:01:02+02:00 。然后使用 正则表达式 来获取所需时间的数据。按yyyy-mm-dd格式获取当前日期的字符串,通过变量 $time 来设置日志文件名,这行旦样当日期者带没改变时,日志文件名就会更新,nginx会以新的文件名记录日志,实现按天保存日志的效果。

实现过程并不顺利,主要遇到了以下几个问题。

1. access_log 可以通过变量配置文件名,但是 error_log 用同样的方法配置后不生效,不过 error_log 的记录相对较少,目前还不是问题,暂时放弃。

2.保证日志目录可被 nginx 用户组有读写权限,这个踩坑了,配置后日志目录下没有出现日志,nginx.conf里配置的用户名是 nginx ,改了权限之后就可以正常生成了。

python 的 logging 模块, 在多线程应用中, logging.hanlders.TimedRotatingFileHandler 不能正常按日期分割。

解决办法为:重写FileHandler类,用于多线程中日志按天分割。

之后,Logger定义日志的各种参数(格式等):

实例化日志:

中文打字太慢,所以就用英文回答。

The answer by the first respondent is totally wrong. ARCHIVELOG/NONARCHIVELOG has nothing to do with what you ask for. ARCHIVELOG/NONARCHIVELOG is about the recoverbility of the database, but what you want is "Auditing" in Oracle, which is about monitoring users' activities in database.

The auditing mechanism for Oracle is extremely flexible, so I'll only talk about performing full auditing on a single user.

1. Database server setup

To allow auditing on the server you must:

Set "audit_trail = true" in the init.ora file.

Run the $ORACLE_HOME/rdbms/admin/cataudit.sql script while connected as SYS.

2. Audit Options

Assuming that the "AAA" user is to be audited:

CONNECT sys/password AS SYSDBA

AUDIT ALL BY AAA BY ACCESS

AUDIT SELECT TABLE, UPDATE TABLE, INSERT TABLE, DELETE TABLE BY AAA BY ACCESS

AUDIT EXECUTE PROCEDURE BY AAA BY ACCESS

These options audit all DDL and DML issued by "AAA", along with some system events.

DDL (CREATE, ALTER &DROP of objects)

DML (INSERT UPDATE, DELETE, SELECT, EXECUTE).

SYSTEM EVENTS (LOGON, LOGOFF etc.)

3. View Audit Trail

The audit trail is stored in the SYS.AUD$ table. It's contents can be viewed directly or via the following views:

DBA_AUDIT_EXISTS

DBA_AUDIT_OBJECT

DBA_AUDIT_SESSION

DBA_AUDIT_STATEMENT

DBA_AUDIT_TRAIL

DBA_OBJ_AUDIT_OPTS

DBA_PRIV_AUDIT_OPTS

DBA_STMT_AUDIT_OPTS

The audit trail contains a lot of data, but the following are most likely to be of interest:

Username: Oracle Username.

Terminal: Machine that the user performed the action from.

Timestamp:When the action occured.

Object Owner: The owner of the object that was interacted with.

Object Name: The name of the object that was interacted with.

Action Name: The action that occured against the object. (INSERT, UPDATE, DELETE, SELECT, EXECUTE)

4. Maintenance

The audit trail must be deleted/archived on a regular basis to prevent the SYS.AUD$ table growing to an unnacceptable size.

5. Security

Only DBAs should have maintenance access to the audit trail. Auditing modifications of the data in the audit trail itself can be achieved as follows:

AUDIT INSERT, UPDATE, DELETE ON sys.aud$ BY ACCESS

另外,团IDC网上有许多产品团购,便宜有口碑


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12550860.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存