From time to time I see in the projects logging through print by this type:

 print('The action 1 has happened') print('The action 2 has happened') 

Then, simply via cron , the output stream is redirected to some folder.

They run like this:

 */5 * * * * /home/user/project/venv/bin/python /home/user/project/script.py >> /home/user/project/logs/script.log 

How reasonable is it?

Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer to the participants jfs , Denis Bubnov , user194374, fori1ton , aleksandr barakin 1 Feb '17 at 13:49 .

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Add to the question an example of redirecting the output stream, which is in CRON. - Egor Smolyakov
  • @EgorSmolyakov did - faoxis
  • one
    What for? print is not output to the file, use logging - users
  • 3
    there is no objective answer. Based on the principle that you should use the simplest solution that works, then print() is quite suitable — simple and can work even when more complex logging code that writes to a file is less convenient, for example, for docker logs . - jfs
  • one
    @jfs but because if you want the record to be not in the file, you can not show the file to the logger and the output will be the same as with print , but with a bunch of advantages. And not to say that the entry logging.info('something') much more complicated than print('something') . - faoxis

2 answers 2

Your option is great for solving the output of the script to the file.


But if in the future it becomes necessary to divide the output by type: error or information and to increase the readability of the logs, you can use logging for the place print .

This will allow you to output directly to the file and the screen, it is possible to easily track when the output was and where.

 import logging import logging.handlers logger = None def set_logging(): global logger logger = logging.getLogger("main") logger.setLevel(logging.DEBUG) formatter = logging.Formatter('%(filename)s[%(lineno)-2d] %(levelname)-1s [%(asctime)s] %(message)s') file_logger = logging.handlers.RotatingFileHandler("adduser.log", maxBytes=10000000, backupCount=5) file_logger.setLevel(logging.DEBUG) file_logger.setFormatter(formatter) console_logger = logging.StreamHandler() console_logger.setLevel(logging.DEBUG) console_logger.setFormatter(formatter) logger.addHandler(file_logger) logger.addHandler(console_logger) set_logging() logger.debug("DEBUG INFO") logger.error("ERROR INFO") logger.warning('WARNING INFO') logger.info('INFO?') 

Conclusion:

 test.py[21] DEBUG [2017-01-27 14:13:25,600] DEBUG INFO test.py[22] ERROR [2017-01-27 14:13:25,601] ERROR INFO test.py[23] WARNING [2017-01-27 14:13:25,601] WARNING INFO test.py[24] INFO [2017-01-27 14:13:25,602] INFO? 
     # print удобен для чегото простого + делает перенос строки, берет несколько аргументов for nums in [1, 2, 3], [4, 5, 6]: print(*nums, file=open('file.txt', 'a'))