简体   繁体   中英

How to save log file using Python?

I have a powershell script and I save the log using this way

$Log = Start-Transcript -Path $Log_Path -Force

How do I use it in Python?

You can write/save the log file using the below command.

logging.basicConfig(filename='path to the log file', level=...)

There is a filemode option to overwrite the file. To overwrite you can specifie filemode:w

logging.basicConfig(filename='logs.log',
            filemode='w',
            level=logging.INFO)

filemode: Specifies the mode to open the file, if filename is specified (if filemode is unspecified, it defaults to 'a').

There is a logging module in python. You can import an use that.

import logging logging.basicConfig(level=logging.DEBUG) logging.debug('This will get logged')

And the output will be:

DEBUG:root:This will get logged

Similarly, you can use the other log levels too. You can find more about it here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM