1

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

1
  • There isn't a clear-cut way to do that, but you can jerry-rig something using Python's built-in log module Commented Jun 7, 2016 at 18:11

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

tried this code in IPython. I do not see the log file.log file in my python folder? Is there any specific addition like handlers I need to add to make the log file ? Thanks
0

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)

5 Comments

I want the output to include the variable name as well . example z=2016-06-07
Manipulate the object . Pretend the string before the string object.
logging.debug("x=" + str(x))
I tried the following code. But I see the example.log is empty
I do not understand the logging model completely. But what I see is that when I execute from my IPython note book, I see the log file in the folder but it is empty. I see the logger object is created, but not sure about the actual log file. Can you try this in IPython?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.