-1

I have a Python file test.py and its output is 5 lines. I want to redirect the output (these 5 lines) into a log file. Is there a way to run the test.py and redirect directly the output as a log-file (test.log) in Python?

So if I have:

print(blabla)
print(blablo)

Then the test.log will look like:

09/03/20 15:23:56:019 blabla
09/03/20 15:23:56:067 blablo
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
carl
  • 388
  • 2
  • 19
  • If you mean without changing the file than it's really not a Python question. Using Linux you can do `python test.py > test.log`. Otherwise... – Tomerikoo Dec 06 '20 at 11:30
  • Does this answer your question? [How to redirect 'print' output to a file using python?](https://stackoverflow.com/questions/7152762/how-to-redirect-print-output-to-a-file-using-python) – Tomerikoo Dec 06 '20 at 11:31
  • @Tomerikoo. I tried this `python test.py > test.log`, but `test.log` is missing the timestamp. – carl Dec 06 '20 at 11:33
  • Then what's wrong with using `logging` as suggested by Kalyan? It does exactly what you want. If you don't want to change everywhere it says `print` you can just do at the top of your program `print = logger.info` or something similar – Tomerikoo Dec 06 '20 at 11:52
  • The code form Kaylan is how to make a log file from a scratch. My problem is how to redirect the output of a `python-file` to be a `file.log` with the logging format. – carl Dec 06 '20 at 11:56
  • I don't see the difference... You can use `filemode='a'` instead of `filemode='w'` – Tomerikoo Dec 06 '20 at 11:58
  • under `logging.basicConfig(filename="test.log")`the `test.log` will be a log-file, but for me I have as a start `test.py` – carl Dec 06 '20 at 12:01
  • `test.py` is the Python file you are actually running. `test.log` is the log file you create. You got me really confused now. Can you explain in the question why `logging` doesn't solve your problem? – Tomerikoo Dec 06 '20 at 12:05
  • I need to use `test.py` to make the `test.log`. The `test.log` has to be as log-file format. The code from Kalyan create just `test.log` with a lines from `logger.debug, logger.info`..etc. – carl Dec 06 '20 at 12:36
  • Ok, we're going in a loop. Use `logging` in your `test.py` to create `test.log`... – Tomerikoo Dec 06 '20 at 12:38
  • This my problem. How to put the output of my `test.py` to make `test.log`. Even with `logging` it is not obvious. – carl Dec 06 '20 at 12:41
  • So please explain why Kalyan's answer doesn't do what you want because it certainly seems so – Tomerikoo Dec 06 '20 at 12:42
  • For example: where in `logging` to add `tets.py`? and how the output of `test.py` will be in `test.log`? I understand the timestamp using `logging`. – carl Dec 06 '20 at 12:44
  • What do you mean? You need to `import logging` inside `test.py`. Is your question how to do that ***without modifying `test.py`***? – Tomerikoo Dec 06 '20 at 12:45
  • I tried your suggestion (I am not getting what I want). I want to log what the `print()` methods (inside of `test.py`) print out, like using `py test.py > test.log `. – carl Dec 06 '20 at 12:53
  • Well look I'm going to go down from this Merry-Go-round ride. If you want, [edit] the question with a clear description of what's wrong with using `logging`, providing a [mre] of your code with its output and the output you're aiming for with any restrictions you have. Right now I really can't understand how come `logging` (with the proper customization) doesn't give you what you need. I will check up if you edit your question but will not participate in this loop-conversation – Tomerikoo Dec 06 '20 at 12:58
  • I find the solution, thanks to you (when you sad to use `logging` inside of `test.py`) and @Kalyan. I misunderstood how to use `logging`. – carl Dec 06 '20 at 13:23
  • Ok I'm happy to hear and glad I could help in any way... Sorry for the confusion, happy you can ask your questions more clearly in the future ^_^ It would help if you had an actual [mre] in the question – Tomerikoo Dec 06 '20 at 13:26

2 Answers2

1

You can use Python logging module.

#importing module
import logging

#Create and configure logger
logging.basicConfig(filename="test.log",
                format='%(asctime)s %(message)s',
                filemode='w')

#Creating an object
logger=logging.getLogger()

#Setting the threshold of logger to DEBUG
logger.setLevel(logging.DEBUG)

#Test messages
logger.debug("Debug Message")
logger.info("Just an information")
logger.warning("Its a Warning")
logger.error("Error message")
logger.critical("Critical level message")

test.log File output

Ref: https://www.geeksforgeeks.org/logging-in-python/

  • This code create a log file. I am looking for to redirect the ouput of a python-file as a log file. please read again my question. – carl Dec 06 '20 at 11:30
0

You can use the file parameter for print.

For example:

f=open('/tmp/example.txt','x')
print('sample_text',file=f)

You might want to use logging instead.

user0
  • 138
  • 1
  • 2
  • 6