gpt4 book ai didi

batch-file - 根据日志文件的输出字符串更改文本颜色的批处理文件

转载 作者:行者123 更新时间:2023-12-04 01:40:20 27 4
gpt4 key购买 nike

我正在尝试使用批处理文件读取日志文件。

  • 如果批处理文件在日志文件中发现SUCCESS,则文本应为绿色。
  • 如果批处理文件在日志文件中发现WARNING,则文本应为青色。
  • 如果批处理文件在日志文件中发现ERROR,则文本应为红色。

当它找到一个值中的任何一个时它会起作用,但是如果日志文件包含两个或多个不同的结果,如 SUCCESSWARNING 它不起作用.

尝试在 Windows 上使用批处理文件读取日志文件。

@echo off
set LogPath=C:\Mworks\logs
set /p MM-DD="Enter Month and Day (MM-DD) to Search logs for success close eg. 08-24: "
set YEAR=2019
@echo searching for %LogPath%\%YEAR%-%MM-DD%*.log
FOR /F "eol=- tokens=1-8 delims= " %%i in ('find /I "LOG ROTATE COMPLETE" %LogPath%\%YEAR%-%MM-DD%*.log') do set result=%%p
echo %result%

(
IF /I %result%==ERROR (goto :ERROR)
)

(
if /I %result%==SUCCESS (goto :SUCCESS)
)

(
if /I %result%==WARNING (goto :WARNING)
)

REM somehow need to catch value at token 8 and color the lines accordingly
REM proper use of enableDelayedExpansion might help but it's quite tough simply with batch script.
REM I've seen hackoo's version of pinger doing it but the code is hard to understand, which part controls what.
:SUCCESS
color 0A
REM this line needs to be on GREEN
FOR /F "eol=- tokens=1-8 delims= " %%i in ('find /I "LOG ROTATE COMPLETE" %LogPath%\2019-%MM-DD%*.log') do @echo %%i %%j %%k %%l %%m %%n %%o %%p
goto end

:ERROR
color 0C
REM this line nees to be on RED
FOR /F "eol=- tokens=1-8 delims= " %%i in ('find /I "LOG ROTATE COMPLETE" %LogPath%\2019-%MM-DD%*.log') do @echo %%i %%j %%k %%l %%m %%n %%o %%p
goto end
:WARNING
REM This line needs to be on CYAN
color 0B
FOR /F "eol=- tokens=1-8 delims= " %%i in ('find /I "LOG ROTATE COMPLETE" %LogPath%\2019-%MM-DD%*.log') do @echo %%i %%j %%k %%l %%m %%n %%o %%p
:end
pause

如果代码找到多个结果,其中结果可能是 SUCCESSWARNINGERROR,则代码不起作用。

    **LOG BEGUN     2019-08-24 03:42:28,662
loading c:Mworksconfiglog4j2.xml
INFO 2019-08-24 03:42:34,100 Initializing configs... :: oracle.retail.mworks.config.mworksProperties [mworks]
INFO 2019-08-24 03:42:34,100 Loading Properties - jar:file:/C:/Mworks/lib/menv-engine.jar!/dtv/res/config/actions.properties :: dtv.util.ResourceUtils [mworks]
INFO 2019-08-24 03:42:34,115 Loading Properties - file:/C:/Mworks/cust_config/version1/actions.properties :: dtv.util.ResourceUtils [mworks]
INFO 2019-08-24 03:42:34,131 Loading Properties - jar:file:/C:/Mworks/lib/menv-engine.jar!/dtv/res/config/environment.properties :: dtv.util.ResourceUtils [mworks]
INFO 2019-08-24 03:42:34,131 Loading Properties - file:/C:/Mworks/cust_config/version1/environment.properties :: dtv.util.ResourceUtils [mworks]
INFO 2019-08-24 03:42:34,146 Loading Properties - jar:file:/C:/Mworks/lib/menv-engine.jar!/dtv/res/config/update.properties :: dtv.util.ResourceUtils [mworks]
INFO 2019-08-24 03:42:34,162 Loading Properties - file:/C:/Mworks/cust_config/version1/update.properties :: dtv.util.ResourceUtils [mworks]
INFO 2019-08-24 03:42:34,162 Loading Properties - jar:file:/C:/Mworks/lib/menv-engine.jar!/dtv/res/config/local.properties :: dtv.util.ResourceUtils [mworks]
INFO 2019-08-24 03:42:34,162 Loading Properties - file:/C:/Mworks/cust_config/version1/local.properties :: dtv.util.ResourceUtils [mworks]
INFO 2019-08-24 03:42:34,584 Loading registration data from c:\Mworks\res\data\registrationdata.json :: oracle.retail.mworks.registration.RegistrationDataManager [mworks]
INFO 2019-08-24 03:42:35,287 Gathering local Client data. :: oracle.retail.mworks.registration.RegistrationDataManager [mworks]
INFO 2019-08-24 03:42:36,334 loading jar:file:/C:/Mworks/lib/menv-engine.jar!/dtv/res/config/MBeanInfoConfig.xml :: dtv.util.config.ConfigHelper [mworks]
INFO 2019-08-24 03:42:36,883
INFO 2019-08-24 03:42:36,883 Waiting for services to start... :: oracle.retail.mworks.mworks [mworks]
ntly running actions: [startup-lead, create-update-directories, LOG ROTATE] :: oracle.retail.mworks.action.Action [ActionExec-1]
INFO 2019-08-24 03:42:40,447 Action [CreateUpdateDirectories :: oracle.retail.mworks.atoms.CreateUpdateDirectories] complete. State: SUCCESS, Result: -----------------------------------
The text below should be in RED
----------------------------------
INFO 2019-08-24 03:42:40:03,060 LOG ROTATE complete. Status: ERROR Created update directories. :: oracle.retail.mworks.atoms.Atom [ActionExec-1]


INFO 2019-08-24 03:42:40,447 Currently running actions: [startup-lead, LOG ROTATE] :: oracle.retail.mworks.action.Action [ActionExec-1]
INFO 2019-08-24 03:42:40,447 Action [create-update-directories] returned state [SUCCESS] with message [Created update directories.] ::

The text below should be in cyan
---------------------------------------
INFO 2019-08-24 04:44:03,060 LOG ROTATE complete. Status: WARNING

LOT OF lines DELETED

The text below should be in green
----------------------------------------
INFO 2019-08-24 05:44:03,060 LOG ROTATE complete. Status: SUCCESS :: oracle.retail.xenvironment.action.Action [ActionExec-2]
sample log

This is my mono color output

像这样:

@Echo Off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
call :chooseColor 0A "This is colored Green means SUCCESS"
echo.
call :chooseColor 0B "This is colored Cyan means WARNING"
echo.
call :chooseColor 0C "This is colored Red means ERROR"
echo.
pause
goto eof
:chooseColor
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
:eof

output should be like this

最佳答案

使用 ANSI 转义码的纯批处理解决方案需要更新的 Win10 版本。
困难是 ESC 符号十六进制 0x1b,dec 27,并非所有编辑器都能轻松提供。

此处使用 certutil 将十六进制转换为二进制已解决。

EDIT 用小写的 %%f 将 for 堆叠到 for/f

:: Q:\Test\2019\08\31\SO_57736435.cmd
@echo off
:restart
set "MM-DD="
set /p MM-DD="Enter Month and Day (MM-DD) to Search logs for success close eg. 08-24: "
if not defined MM-DD Exit /B 0

set "LogPath=C:\Mworks\logs"
set "File=%LogPath%\log.2019-%MM-DD%*.log"
if not exist "%File%" (Echo %File% doesn't exist&pause&goto :restart)

:: ANSI Escape codes, supported with Win10 again
Call :GetCSI

FOR %%f in ("%File%") Do FOR /F "usebackq tokens=1-8* delims= " %%A in ("%%f"
) do if /I "%%A%%D%%E%%F%%G"=="INFOLogRotateComplete.Status:" (
Call :%%H "%%A %%B %%C %%D %%E %%F %%G %%H %%I"
) Else (
Echo:%%A %%B %%C %%D %%E %%F %%G %%H %%I
)
Pause
exit /B 0

:Warning
Echo(%CSI%36m%~1%CSI%0m
Goto :Eof

:Success
Echo(%CSI%32m%~1%CSI%0m
Goto :Eof

:Error
Echo(%CSI%31m%~1%CSI%0m
Goto :Eof

:GetCSI
echo 1B 5B>CSI.hex
Del CSI.bin >NUL 2>&1
certutil -decodehex CSI.hex CSI.bin >NUL 2>&1
Set /P CSI=<CSI.bin
Goto :Eof

enter image description here

关于batch-file - 根据日志文件的输出字符串更改文本颜色的批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57736435/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com