切割tomcat日志 bat脚本
时间: 2023-07-22 21:15:21 浏览: 141
Shell脚本切割tomcat的日志文件
下面是一个基本的切割Tomcat日志的batch脚本:
```
@echo off
setlocal enabledelayedexpansion
set "logdir=C:\Tomcat\logs"
set "logname=catalina"
set "logext=.log"
set "maxsize=50000000"
pushd "%logdir%"
for /f "tokens=1-3 delims=: " %%a in ('time /t') do (
set "hour=%%a"
set "minute=%%b"
set "second=%%c"
)
set "timestamp=!hour!-!minute!-!second!"
set "logfile=%logname%_%timestamp%%logext%"
if not exist "%logfile%" (
copy "%logname%%logext%" "%logfile%"
type nul > "%logname%%logext%"
)
for /f "usebackq tokens=1,2 delims==" %%a in (`wmic OS Get localdatetime /value`) do (
if '.%%a'=='.LocalDateTime' (
set "datetime=%%b"
set "date=!datetime:~0,4!-!datetime:~4,2!-!datetime:~6,2!"
)
)
for %%f in ("%logname%_*%logext%") do (
if %%~zf GTR %maxsize% (
set "num=%%~nf"
set "num=!num:*_=!"
set /a "num+=1"
set "newname=%logname%_!date!_!num!%logext%"
ren "%%~ff" "!newname!"
copy "%logname%%logext%" "%logfile%"
type nul > "%logname%%logext%"
)
)
popd
```
此脚本将在Tomcat日志文件夹中查找名为“catalina.log”的日志文件,并将其切割为50MB大小的文件。每当日志文件达到最大大小时,它将重命名为类似于“catalina_2022-01-01_1.log”的格式,其中“2022-01-01”表示当前日期,并在文件名中添加数字以标识它们。新的日志文件将以“catalina_HH-MM-SS.log”的格式创建,并将原始日志文件清空以开始新的日志。请注意,此脚本仅适用于Windows操作系统。
阅读全文