0

I want to create a log file for my python script. I have already used print to debug the script. Since it is a large file, it is difficult to re-edit the print with date.

So I need to redirect the print to a file with the printing date and time. I have already used the command

python -u test.py | sed -u "s/^/`date +'[%a %b %d %k:%M:%S]'` /" >> test.log

But this command prepends the date at which it is run. It's not the printing time.

I need to have a file with the printing time not the time at which the script is run.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
mcv
  • 45
  • 2
  • 10
  • 1
    You should consider learning about proper logging facilities in Python instead of just using `print()` ;-) But for an easy fix, try this: `python -u test.py | while IFS="" read -r line; do printf "%s: %s\n" "$(date +%FT%T)" "$line"; done`. – Alfe Aug 31 '18 at 08:00
  • Not an answer to your question, but as Alfe said you should look into a proper logger instad: https://stackoverflow.com/questions/6386698/using-the-logging-python-class-to-write-to-a-file – Arne Aug 31 '18 at 08:01
  • How apache do? In my website, i only use print function but when we look at the apache log it will contain the time too. – mcv Aug 31 '18 at 08:42

0 Answers0