nginx日誌分割:windows和linux

來源:互聯網
上載者:User

標籤:nginx日誌分割   linux   windows   

一、為什麼對日誌進行分割。

1.nginx日誌預設情況下統統寫入到一個檔案中,檔案會變的越來越大.

2.單個的記錄檔非常不方便查看分析。

二、簡析日誌分割。

不論是windows還是linux,對日誌的分割都是一條思路。即:

1.對現有記錄檔進行重新命名。

2.產生新的記錄檔。

3.定製指令碼,定時執行.

三、日誌分割實操。

windows系統:

1.logcut.bat

#定義時間(年月日)

for /f "tokens=1 delims=/ " %%j in ("%date%") do set d1=%%j

for /f "tokens=2 delims=/ " %%j in ("%date%") do set d2=%%j

for /f "tokens=3 delims=/ " %%j in ("%date%") do set d3=%%j

#建立目錄便於查看

set backupdir=C:\nginx\logs\%d1%\%d2%\%d3%

mkdir %backupdir%

#移動原有日誌,相當於重新命名

move F:\nginx\access.log %backupdir%

#重開日誌,產生新的記錄檔

F:\nginx\nginx.exe -s reopen

2.通過windows的計劃任務來實現定時分割日誌(本人使用的windows2012 r2)

linux系統:

1.logcut.sh

#!/bin/sh

#定義日誌格式

Dateformat=`date +%Y%m%d`

#定義日誌目錄

Basedir="/application/nginx"

Nginxlogdir="$Basedir/logs"

#定義分割的日誌

Logname="access"

#判斷是否存在記錄檔

[ -d $Nginxlogdir ] && cd $Nginxlogdir||exit 1

[ -f ${Logname}.log ]||exit 1

#對原有的記錄檔進行修改

/bin/mv $Logname.log ${Dateformat}_$Logname.log

#重建nginx日誌

$Basedir/sbin/nginx -s reload

2.定時任務,實現功能。

crontab -l

#logs  cut

00 00  * * * sh /server/scripts/logcut.sh  &>/dev/null








nginx日誌分割:windows和linux

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.