pmv
pmv

Reputation: 357

redirecting python output to a log file

I have a simple request. I have a python file with a list of variables. I want to execute the python file and get the output of that written to the log file. What is the simple way to do this? Example: var.py has the following code

x = (10,11,12)
y = str("case when id =1 then gr8 else ok end")
z = datetime.datatime.today().strftime('%Y-%m-%d')

I want the log to show the variables resolution in the same order

x = (10,11,12)
y = 'case when id =1 then gr8 else ok end'
z = 2016-06-07

How can I accomplish this in python?

This is what I tried

# In:
import logging

# set root logger level
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)

# setup custom logger
logger = logging.getLogger(__name__)
handler = logging.FileHandler('example.log')
handler.setLevel(logging.INFO)
logger.addHandler(handler)

# log
x = (10,11,12)
y = str("case when id =1 then gr8 else ok end")
logger.debug(x)
logger.debug(y)

example.log file is empty

Upvotes: 1

Views: 1864

Answers (2)

dot.Py
dot.Py

Reputation: 5157

Input/Output Operations with Python is easy.

You can open, write and close a file manually, ie.:

text_file = open("OutputFile.txt", "w")
text_file.write("Write blablabla into a file")
text_file.close()

Or you use a context manager (the file is closed automatically for you), ie.:

This is generally a better coding practice..

with open("Output.txt", "w") as text_file:
    text_file.write("Write blablabla into a file")

In your example:

import datetime

x = (10,11,12)
y = str("case when id =1 then gr8 else ok end")
z = datetime.datetime.today().strftime('%Y-%m-%d')

outfile = 'outputfile.txt'

with open(outfile, 'w') as f:
    f.write(str(x))
    f.write("\n")
    f.write(y)
    f.write("\n")
    f.write(z)
    f.write("\n")

Produces a file called outputfile.txt in the script folder, with the following lines:

(10, 11, 12)
case when id =1 then gr8 else ok end
2016-06-07

But if you want a logging specific library, you can take a look at LOGGING.

import datetime, logging

logfile = 'logfile.log'

logging.basicConfig(filename=logfile, 
                    level=logging.INFO,
                    format='%(asctime)s.%(msecs)03d %(levelname)s %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S')

x = (10,11,12)
y = str("case when id =1 then gr8 else ok end")
z = datetime.datetime.today().strftime('%Y-%m-%d')

logging.info(x)
logging.info(y)
logging.info(z)

This will produce the following output:

2016-06-07 15:28:12.874 INFO (10, 11, 12)
2016-06-07 15:28:12.874 INFO case when id =1 then gr8 else ok end
2016-06-07 15:28:12.874 INFO 2016-06-07

Upvotes: 1

prateek05
prateek05

Reputation: 503

Use the python logger functionality

import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
x = (10,11,12)
y = str("case when id =1 then gr8 else ok end")
logging.debug(x)
logging.debug(y)

Upvotes: 0

Related Questions