How to Create a Script Wrapper That Logs Everything Automatically in Batch Script
One of the limitations of standard Batch scripting is that once a script finishes, its output vanishes from the command prompt. If you need to debug a failure that happened at 3:00 AM, you have no way to see what the script "said" unless you manually added >> log.txt to every single line of code, a tedious and error-prone task. A "Log Wrapper" is a parent script that launches your main logic and transparently redirects all output (Success and Errors) into a file in real-time.
This guide will explain how to build a universal wrapper that captures everything without modifying your original script.
Method 1: The Parent Redirector (The Easiest Way)
Instead of running myscript.bat, you run a wrapper.bat that handles the output stream logic.
The Wrapper Template
@echo off
setlocal
set "LogFile=%~dp0output_audit.log"
echo [%date% %time%] --- Starting Session --- >> "%LogFile%"
:: Call the main script and group all its output (1 = stdout, 2 = stderr)
:: The brackets ( ) treat the entire block as a single stream for redirection.
(
call "my_heavy_task.bat"
) >> "%LogFile%" 2>&1
set "ExitCode=%errorlevel%"
echo [%date% %time%] --- Session Ended with code %ExitCode% --- >> "%LogFile%"
endlocal & exit /b %ExitCode%
Why this works:
2>&1: This is a critical operator. It tells Windows to take "Stream 2" (Standard Error) and pipe it into "Stream 1" (Standard Output). This ensures that even "Access Denied" or "File Not Found" messages are saved to the log.- The Brackets: By wrapping the
callinside( ), we ensure that every line executed inside that call is sent to the audit log.
Method 2: The Self-Logging Wrapper (Single File)
If you want to keep everything in one file, you can have the script "Relaunch" itself into a log file if it detects it hasn't been logged yet.
@echo off
setlocal
:: If the first argument is '--logged', skip the redirector and run the logic
if "%~1"=="--logged" (
shift
goto :MainLogic
)
:: Relaunch the script with the --logged flag and redirect everything to a file
set "LogFile=%~dp0audit.txt"
echo [SYSTEM] Starting logs in %LogFile%...
"%~f0" --logged %* >> "%LogFile%" 2>&1
endlocal & exit /b %errorlevel%
:MainLogic
echo [INFO] Running core tasks...
dir C:\Windows\System32\config
echo [DONE] Tasks finished!
endlocal
exit /b 0
Method 3: Real-Time Logging with TEE (PowerShell)
Standard Batch redirection (>>) saves the output but hides it from the user's screen. If you want to see the text and save it at the same time, you can use the Tee-Object feature in PowerShell.
@echo off
setlocal
set "log=%~dp0session.log"
echo [SYSTEM] Launching with real-time logging...
powershell -Command "cmd /c '.\my_script.bat' 2>&1 | Tee-Object -FilePath '%log%' -Append; exit $LASTEXITCODE"
endlocal & exit /b %errorlevel%
The name "Tee" comes from a T-shaped pipe. It splits the data stream so it goes to two places at once: your monitor and your log file.
How to Avoid Common Errors
Wrong Way: Using a single >
If you use > log.txt, you will delete your log history every time you start a new session.
Correct Way: Use >> log.txt for the wrapper. This "Appends" the data to the bottom of the file, keeping your history intact.
Problem: Infinite Loops
In Method 2 (Self-Logging), failing to check for the --logged flag will cause the script to keep relaunching itself into a log file forever until it crashes the computer.
Best Practice: Always use an unique identifier at the start of the script to distinguish between the "Wrapper Phase" and the "Working Phase."
Best Practices and Rules
1. Timestamping
Always include the date and time in your wrapper's header and footer. A log file without timestamps is useless for determining exactly when a crash occurred.
2. Relative Paths
Store your log files in a specific directory like %~dp0logs. Don't let your logs clutter the desktop or the system root.
3. Log Rotation
If a script runs every 5 minutes, your log file will eventually become massive. Periodically check the size of the log and delete it or move it if it exceeds 10MB.
Conclusions
Building a log wrapper is one of the most proactive steps you can take to make your Windows automation professional. By capturing every detail, from successful command outputs to catastrophic system errors, you eliminate the mystery of "What happened?" This transparency ensures that your scripts are accountable, audit-ready, and significantly easier to debug in production environments.