Job
Job

Reputation: 505

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?

Upvotes: 1

Views: 9157

Answers (2)

Yashi Aggarwal
Yashi Aggarwal

Reputation: 405

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’).

Upvotes: 4

Nitin Sharma
Nitin Sharma

Reputation: 57

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

Upvotes: 1

Related Questions