1

I have a jar file which I trigger using this batch file and want everything that is shown on the command line window to be logged along with the timestamp. I need to write the timestamp in each line that is written inside the logs. Like this:

Tue 06/28/2016,15:42:22.24 -  logssssss
Tue 06/28/2016,15:42:22.24 -  logssssss
Tue 06/28/2016,15:42:22.24 -  logssssss
...

I have the following code:

@echo off
echo %date%,%time% - %~1 >>output.log
call :sub >>output.txt
echo %date%,%time% - %~1 >>output.log
exit /b

:sub
command1
command2
...
commandN

Using this I am only able to record the start and end time of the script.

Edit 1: I want the script to display the execution on screen along with writing in a file.

Edit 2: Now, I have the following code but it does not write the logs along with execution. Please tell me where to improve.

@echo off
setlocal enabledelayedexpansion

SET LogFile=D:\Logs\logfile.out
SET Logg=^> tmp.out^&^& type tmp.out^&^&type tmp.out^>^>%LogFile%

if /I "%~1"=="Recursive" java Test
prompt $D,$T$S-$S
>> %Logg% echo !date!,!time! - %~1
>> %Logg% (for /F delims^=^ eol^= %%$ in ('^(for /F delims^^^=^^^ eol^^^= %%# in ^('"%~f0" Recursive'^) do rem/ %%#^)') do echo %%$)

>> %Logg% echo !date!,!time! - %~1
prompt
exit /b
  • Perhaps this is an option: don't use `@echo off` but write `@prompt $D,$T - ` instead, so you'll receive command echoes prefixed with date/time string prompts... – aschipfl Jun 28 '16 at 13:57
  • I assume you mean `call :sub >>output.log` (you wrote `output.txt`), don't you? – aschipfl Jun 28 '16 at 14:24

2 Answers2

1

The eaiest way is to use a logging framework like logback, log4j, etc. But you'll need refactor your code.

  • Thank you @marco-a-hernandez for your answer. I have tried using log4j but it **slows** down the **execution**. – Ishan Saini Jun 30 '16 at 05:00
  • Are you writing the log into a File or writting massive text? If you use the ConsoleAppender the performance should be similar to using System.output. – Marco A. Hernandez Jun 30 '16 at 10:01
  • Actually, I run several automation scripts that are compressed into a runnable jar which run for a prolonged period of time. Thus, I need an efficient way to write all the logs. That is why I decided it using a batch file. – Ishan Saini Jun 30 '16 at 14:02
  • Can u help me with a sample code illustrating console appender? – Ishan Saini Jul 01 '16 at 05:02
  • There are tons of examples. See here http://www.mkyong.com/logging/log4j-hello-world-example/ – Marco A. Hernandez Jul 01 '16 at 07:22
1

Before I provide some suggestions, I want to mention that you should put the redirection in front of the command rather than behind in order to avoid trailing spaces or other trouble, and to put quotation marks around any paths in order to avoid trouble with white-spaces or special characters, like this:

>>"output.log" echo %date%,%time% - %~1

But now for the solutions...

You could make use of command echos and a custom prompt holding date/time information:

@echo off
>> "output.log" echo %date%,%time% - %~1
>> "output.log" call :sub
>> "output.log" echo %date%,%time% - %~1
exit /b

:sub
prompt $D,$T$S-$S
echo on
command1
command2
...
commandN
@echo off
prompt

So you will get the following log entry:

28-Jun-2016,16:33:31.16 - 

28-Jun-2016,16:33:31.16 - command1 
logssssss  <output of command1>

28-Jun-2016,16:33:31.82 - command2 
logssssss  <output of command2>

28-Jun-2016,16:33:32.28 - ... 
logssssss  <output of ...>

28-Jun-2016,16:33:36.07 - commandN 
logssssss  <output of commandN>
28-Jun-2016,16:33:36.07 - 

All the empty lines are caused by the command echoing.
Note that the date/time information depends on the current locale settings.

To get rid of the command echoes and just receive the command outputs with date/time preceded, you could use a for /F loop to capture the command outputs and force the date/time prompt prefix with the rem command. But you cannot call a sub-routine like call :sub then, because commands within for /F are executed in another cmd instance which is not run in a batch file context; also any environment changes within that cmd instance is not reflected in the instance where this batch file is running in. Furthermore, you will receive the log string of the entire :sub section once when it has finished rather than receiving it after every command.
If all this is no problem for you, you might want to use the following code:

@echo off
if /I "%~1"=="Recursive" goto :sub
prompt $D,$T$S-$S
>> "output.log" echo %date%,%time% - %~1
>> "output.log" (echo on & for /F delims^=^ eol^= %%# in ('"%~f0" Recursive') do rem/ %%#)
@echo off
>> "output.log" echo %date%,%time% - %~1
prompt
exit /b

:sub
command1
command2
...
commandN

The call :sub command is replaced by a conditional goto :sub command. When the batch file is called by for /F, an argument Recursive is delivered; if this string is recognised, execution is continued at the :sub section.

The log entry will look like the following:

28-Jun-2016,16:33:31.16 - 

28-Jun-2016,16:33:31.16 - rem/ logssssss  <output of command1> 

28-Jun-2016,16:33:31.82 - rem/ logssssss  <output of command2> 

28-Jun-2016,16:33:32.28 - rem/ logssssss  <output of ...> 

28-Jun-2016,16:33:36.07 - rem/ logssssss  <output of commandN> 
28-Jun-2016,16:33:36.07 - 

To get rid of the empty lines, replace the command line...:

>> "output.log" (echo on & for /F delims^=^ eol^= %%# in ('"%~f0" Recursive') do rem/ %%#)

...by this one...:

>> "output.log" (for /F delims^=^ eol^= %%$ in ('^(for /F delims^^^=^^^ eol^^^= %%# in ^('"%~f0" Recursive'^) do rem/ %%#^)') do echo %%$)

...and remove the second line @echo off immediately after this.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • if you need to to it synchonous, you need a special program. Search for `tee` or `tee for windows`. If assynchronous is ok, write to file, followed by `type file` [here](http://stackoverflow.com/a/15553922/2152082) is a possible implemention. – Stephan Jun 30 '16 at 07:05
  • Hey @aschipfl thank you for your answer but I need to write the logs along with the execution. As the jar triggered runs for hours. Can you help me with a work around – Ishan Saini Jun 30 '16 at 09:59