简体   繁体   English

Apache旋转访问和错误日​​志Windows

[英]Apache rotate Access and Error logs Windows

How can I rotate the Apache Access and Error logs on a Window 2000 box? 如何在Window 2000框上旋转Apache Access和Error日志?

I include my batch file below as an answer. 我在下面提供了我的批处理文件作为答案。

Is there a way of doing this directly via the Apache config file? 有没有办法直接通过Apache配置文件执行此操作? I'm currently using the following customlog command to generate daily logs. 我目前正在使用以下customlog命令生成每日日志。

CustomLog '|" "*Apache-Path/bin/rotatelogs.exe" "*Apache-Path/logs/backup/internet_access_%d-%m-%y.log" 86400' combined CustomLog'|“”* Apache-Path / bin / rotatelogs.exe“”* Apache-Path / logs / backup / internet_access_%d-%m-%y.log“86400'组合

Here's the DOS batch file, modified as annotated. 这是DOS批处理文件,修改为带注释。 I run it weekly and it keeps 8 weeks of zipped backups. 我每周运行它,它保留了8周的压缩备份。 You'll need to install 7 zip. 你需要安装7个拉链。

I haven't parametrised the paths, feel free to. 我没有参数化路径,随意。


@echo off

:: Name - svrlogmng.bat
:: Description - Server Log File Manager
::
:: History
:: Date         Authory    Change
:: 22-May-2005  AGButler   Original
:: 14-Jan-2008  AIMackenzie Changed net stops and paths where necessary

:: ========================================================
:: setup variables and parameters
:: ========================================================

:: generate date and time variables
for /f "tokens=2,3,4 delims=/ " %%i in ('date /T') do set trdt=%%k%%j%%i
for /f "tokens=1,2 delims=: " %%i in ('time /T') do set trtt=%%i%%j
set nftu=%trdt%%trtt%

:: set the Number Of Archives To Keep
set /a noatk=8

:: ========================================================
:: turn over log files
:: ========================================================

:: change to the apache log file directory
cd /D "D:\Program Files\Apache Software Foundation\Apache2.2\logs\"

:: stop Apache Service, Move log files and restart Apache Service
"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -k stop

echo %nftu% >> access.log
move "D:\Program Files\Apache Software Foundation\Apache2.2\logs\access.log" "D:\Program Files\Apache Software Foundation\Apache2.2\logs\%nftu%_access.log"

echo %nftu% >> error.log
move "D:\Program Files\Apache Software Foundation\Apache2.2\logs\error.log" "D:\Program Files\Apache Software Foundation\Apache2.2\logs\%nftu%_error.log"

"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -k start

:: ========================================================
:: zip todays Access and Error log files, then delete old logs
:: ========================================================

:: zip the files
"D:\Program Files\7-Zip\7z.exe" a -tzip %nftu%_logs.zip %nftu%_access.log %nftu%_error.log

:: del the files
del /Q %nftu%_*.log

:: ========================================================
:: rotate the zip files
:: ========================================================

:: make list of archive zip files
type NUL > arclist.dat
for /F "tokens=1,2 delims=[] " %%i in ('dir /B *_logs.zip ^| find /N "_logs.zip"') do echo  %%i = %%j>> arclist.dat

:: count total number of files
for /F "tokens=1 delims=" %%i in ('type arclist.dat ^| find /C "_logs.zip"') do set tnof=%%i

:: setup for and create the deletion list
set /a negtk=%noatk%*-1
set /a tntd=%tnof% - %noatk%

type NUL>dellist.dat
for /L %%i in (%negtk%,1,%tntd%) do find " %%i = " arclist.dat >> dellist.dat

:: del the old files
for /F "tokens=3 delims= " %%i in ('find "_logs.zip" dellist.dat') do del /Q %%i

:: remove temp files
del /Q arclist.dat
del /Q dellist.dat

I've a little bit extend the the bat-script. 我有点扩展了bat脚本。 You can use it for english and german dates. 您可以将它用于英语和德语日期。 You will need 7za.exe at the same directory like script. 您将需要7za.exe在同一目录,如脚本。 The log-dir and -files for rotation are explicitly settable. 可以显式设置用于旋转的log-dir和-files。

@echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
:: ========================================================
:: setup variables and parameters
:: ========================================================

:: USE Service Display Name, Services are space-separated -> Syntax: "Display Name 1" "Display Name 2"
SET ROTATE_SERVICES="Apache2.2"

:: setting LOG-directory, log-files in this directory should be rotate
SET ROTATE_LOGDIR=F:\xampp\apache\logs

:: files which should rotate (space separated)
SET ROTATE_FILES=access.log error.log ssl_request.log

:: SET the Number Of Archives To Keep
SET /a keptarchives=5

:: SET delimiter for date format (english "/", german ".")
SET DATEDEL=.
:: ========================================================
:: DO NOT CHANGE ANYTHING
:: ========================================================

:: Check for existing Log-directory
IF NOT EXIST "%ROTATE_LOGDIR%" (
    CALL :LOG Please check your paths to Log Directory
    PAUSE
    GOTO :EOF
)

:: Check for existing Log-files
FOR %%d IN (%ROTATE_FILES%) DO (
    IF NOT EXIST "%ROTATE_LOGDIR%\%%d" (
        CALL :LOG File %ROTATE_LOGDIR%\%ROTATE_LOGFILES% does not exist!
        PAUSE
        GOTO :EOF
    )
)

:: generate date and time variables for execution
FOR /f "tokens=1,2,3 delims=%DATEDEL% " %%i IN ('date /T') DO SET execdate=%%k%%j%%i
FOR /f "tokens=1,2 delims=: " %%i IN ('time /T') DO SET exectime=%%i%%j
SET fullexectime=%execdate%_%exectime%
:: ========================================================


:: ========================================================
:: Operations
:: ========================================================

FOR %%d IN (%ROTATE_SERVICES%) DO (
    NET STOP %%d
)

FOR %%d IN (%ROTATE_FILES%) DO (
    cd /d %ROTATE_LOGDIR%
    IF NOT EXIST OLD (MKDIR OLD) 
    move %%d %ROTATE_LOGDIR%\OLD\%fullexectime%_%%d
)

FOR %%d IN (%ROTATE_SERVICES%) DO (
    NET START %%d
)

:: ========================================================
:: ZIP - LOGFILES
:: ========================================================
cd /d %ROTATE_LOGDIR%\OLD
CALL "%~dp0\7za.exe" a %ROTATE_LOGDIR%\OLD\%fullexectime%_log.zip %ROTATE_LOGDIR%\OLD\%fullexectime%_*.log
    IF %ERRORLEVEL% NEQ 0 (
        CALL :LOG Error while compressing log-file. Log will not deleted and not rotated. Check your OLD-directory!
        PAUSE
        GOTO :EOF
    )
del /Q %fullexectime%_*.log

:: ========================================================
:: ROTATE - ZIPPED LOGFILES
:: ========================================================

:: make list of archive zip files
type NUL > arclist.dat
for /F "tokens=1,2 delims=[] " %%i in ('dir /B *_log.zip ^| find /N "_log.zip"') do echo  %%i = %%j>> arclist.dat

:: count total number of files
for /F "tokens=1 delims=" %%i in ('type arclist.dat ^| find /C "_log.zip"') do set tnof=%%i

:: setup for and create the deletion list
set /a negtk=%keptarchives%*-1
set /a tntd=%tnof% - %keptarchives%

type NUL>dellist.dat
for /L %%i in (%negtk%,1,%tntd%) do find " %%i = " arclist.dat >> dellist.dat

:: del the old files
for /F "tokens=3 delims= " %%i in ('find "_log.zip" dellist.dat') do del /Q %%i

:: remove temp files
del /Q arclist.dat
del /Q dellist.dat


GOTO :EOF

:LOG
    SET MSG=[%DATE%, %TIME: =0%] %*
    ECHO.%MSG%
    SET MSG=
    GOTO :EOF
pause

I wrote a vbs for a Windows 2003 box once. 我曾经为Windows 2003一个盒子写了一个vbs。 It uses zip.exe for compression (found at info-zip.org) and rotates apache logs and php logs. 它使用zip.exe进行压缩(在info-zip.org上找到)并旋转apache日志和php日志。 Logs are rotated when larger than MAX_SIZE. 当大于MAX_SIZE时,日志会旋转。 It deletes the oldest logs if log count passes MAX_ROTATIONS. 如果日志计数超过MAX_ROTATIONS,它将删除最旧的日志。

Please provide feedback on the script. 请提供有关脚本的反馈。

option explicit

const DEBUG_MODE = false
const MAX_ROTATIONS = 10
const MAX_SIZE = 2097152  ' 2MB
const WEB_LOGS = "c:\path\to\site\logs"
const PHP_LOG = "c:\path\to\phplog"
const APACHE_LOGS = "C:\path\to\Apache2\logs"
const APACHE_SERVICE ="Apache2.2" ' Name of the apache service for restart
const ZIP_APP = "c:\path\to\zip.exe"
const LOGROTATE_LOG = "c:\tmp\logrotate.log"

dim aLogs
aLogs = Array("\error.log","\transfer.log","\sec.log","\phperror.log") 

dim oFSO
set oFSO = CreateObject("Scripting.FileSystemObject")

if (not DEBUG_MODE) then
  dim oLogFile
  set oLogFile = oFSO.CreateTextFile(LOGROTATE_LOG, 2, True)
end if

dim bHasRotated
bHasRotated = false

Print "Starting log rotation"
Print "====================="
ManageWebLogs()
ManageApacheLogs()
ManagePhpLog()

if (bHasRotated = true) then
  Print "====================="
  RestartService APACHE_SERVICE
end if

Print "====================="
Print "Log rotation finished"

if (not DEBUG_MODE) then
  oLogFile.Close
  set oLogFile = nothing
end if
set oFSO = nothing

'*****************************************************************************
' Loop through all the subfolders in the weblog directory
sub ManageWebLogs()
  dim oLogDirs
  set oLogDirs = oFSO.GetFolder(WEB_LOGS)
  dim oLogDir
  for each oLogDir in oLogDirs.SubFolders
      Print "In " & oLogDir.Name
      RotateLogs(oLogDir)
  next 
  set oLogDir = nothing
end sub

'*****************************************************************************
' Loop through the log files in the Apache logs directory
sub ManageApacheLogs()
  dim oLogDir
  set oLogDir = oFSO.GetFolder(APACHE_LOGS)
  Print "In " & oLogDir.Name
  RotateLogs(oLogDir)
  set oLogDir = nothing
end sub

'*****************************************************************************
' Loop through the log files in the Apache logs directory
sub ManagePhpLog()
  dim oLogDir
  set oLogDir = oFSO.GetFolder(PHP_LOG)
  Print "In " & oLogDir.Name
  RotateLogs(oLogDir)
  set oLogDir = nothing
end sub


'*****************************************************************************
' Traverse through each of the log file types and check if they need rotation
sub RotateLogs(ByVal oFolder)
  dim sLog
  dim oLog
  for each sLog in aLogs
    if oFSO.FileExists(oFolder.Path & sLog) then
      set oLog = oFSO.GetFile(oFolder.Path & sLog)
      if (oLog.Size > MAX_SIZE) then
        RotateLog oFolder.Path & sLog
        ArchiveLog oFolder.Path & sLog
        bHasRotated = true
      end if
    end if
  next
  set oLog = nothing
end sub


'*****************************************************************************
' Rotates the given log, by incrementing the file name
sub RotateLog(ByVal sLog)
  dim i
  dim sOldFile, sNewFile
  for i = MAX_ROTATIONS to 1 step -1
    sOldFile = sLog & "." & i & ".zip"
    sNewFile = sLog & "." & (i+1) & ".zip"
    if oFSO.FileExists(sOldFile) and i = MAX_ROTATIONS then
      ' Delete zipfile        
      Print "-- Deleting " & sOldFile
      oFSO.DeleteFile(sOldFile)
    elseif oFSO.FileExists(sOldFile) then
      ' Rename zipfile
      Print "-- Renaming " & sOldFile & " to " & sNewFile
      oFSO.MoveFile sOldFile, sNewFile
    end if
  next
end sub


'*****************************************************************************
' Zips the current log
sub ArchiveLog(ByVal sLog)
  Dim oShell
  Set oShell = CreateObject("WScript.Shell")
  dim sZipFile 
  sZipFile = sLog & ".1.zip"
  Print "-- Archiving " & sLog & " to " & sZipFile
  oShell.Run "cmd /c " & ZIP_APP & " -jq " & sZipFile & " " & sLog, 0, true
  oFSO.DeleteFile(sLog)
  set oShell = nothing
end sub


' ****************************************************************************
' Restarts a given service (in our case Apache)
private sub RestartService( _
  ByVal sService _
)

  Dim oShell
  Set oShell = CreateObject("WScript.Shell")

  ' Service stopped with 'Net' command
  oShell.Run "cmd /c net stop " & sService, 0, true
  Print sService & " service stopped"

  ' Service started with 'Net' command
  oShell.Run "cmd /c net start " & sService, 0, true
  Print sService & " service restarted"

  set oShell = nothing
end sub


'*****************************************************************************
' Echoes out the given message if in debug mode
sub Print(ByVal sMsg)
  if (DEBUG_MODE) then
    wscript.echo sMsg
  else
    oLogFile.WriteLine sMsg
  end if
end sub

As stated I don't think this is strictly a programming question, but AFAIK there is no built-in functionality in Apache to support log rotation. 如上所述,我不认为这是一个严格的编程问题,但AFAIK在Apache中没有内置功能来支持日志轮换。 There's a standard utility on unix systems called logrotate , so that functionality would be redundant on those systems. 在unix系统上有一个名为logrotate的标准实用程序,因此这些系统上的功能将是多余的。

If you rephrase your question to ask how you can write a batch file to handle Apache log rotation on Windows 2000, then answer yourself with your batch file source, that might be helpful to others. 如果您重新解释您的问题以询问如何编写批处理文件来处理Windows 2000上的Apache日志轮换,那么请回答您的批处理文件源,这可能对其他人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Windows上轮换Apache日志文件的最佳方法 - Best way to rotate apache log files on Windows 在Windows中使用Logstash将Apache日志导入Elasticsearch - Import apache logs into elasticsearch using logstash in windows 如何在Windows中旋转tomcat日志? 最好的方法是什么? - How to rotate tomcat logs in Windows? What is the best method? 从 Windows 事件查看器中提取错误日志 - Extracting error logs from Windows event viewer 使用 Apache Cordova + Electron 在 Windows 上访问 Java 库 - Access to Java library on Windows with Apache Cordova + Electron Apache Commons Daemon procrun 访问在 Windows 7 上被拒绝 - Apache Commons Daemon procrun Access is denied on windows 7 访问控制台日志(使用Windows XP的cmd.exe) - Access console logs ( cmd.exe using Windows XP) 在 Windows 上运行 Kafka,但获取日志文件夹的访问被拒绝异常 - Running Kafka on Windows but getting Access denied exception for logs folder 如何配置NSB将错误日志发送到Windows事件查看器? - How to configure NSB to send Error-logs to Windows event viewer? 如何清除Windows事件日志而不显示任何错误消息? - How to clear Windows event logs without showing any error messages?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM