39

I use the Python logging framework with default settings. For some data compare reason:I have to compare the log with other data output. But the python log begin with a default, something like:

INFO:root:post params in transmitter 

Can I set the python log output without INFO:root:, like:

post params in transmitter 

with my own log only?

Thx a lot!

1
  • From: INFO:root:post params in transmitter To: post params in transmitter Commented Dec 2, 2011 at 8:29

5 Answers 5

55

Sure thing. You could set the format to watever you like:

format: '%(message)s' 

Like this:

logging.basicConfig(format='%(message)s', ...) 

See the doc for more info: http://docs.python.org/library/logging.config.html

Sign up to request clarification or add additional context in comments.

1 Comment

Is there an easy way to show INFO messages without levelname, but WARNING and ERROR messages with? It would make it easier to read normal messages, and easy to search errors.
11

Those "INFO:..." or "DEBUG:..." appear there because some handler defines that. My guess: the default handler is still there.

You can check it by taking a peek at logger.handlers right after you created it.

logger = logging.getLogger() logger.handlers = [] # This is the key thing for the question! # Start defining and assigning your handlers here handler = logging.StreamHandler() handler.setLevel(logging.INFO) formatter = logging.Formatter("%(asctime)s: %(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) 

Also, you could just override the format for that default handler:

if (len(logger.handlers) > 0): # Check here whatever. Or try/except. You get the idea... formatter = logging.Formatter("%(asctime)s: %(levelname)s - %(message)s") logger.handlers[0].setFormatter(formatter) 

I am not a Python expert so maybe there is a better way to remove or even not create that default handler but this works pretty well for me.

Note: As stated in the docs, the .basicConfig is useful for simple loggers. If you have multiple streams, with multiple formats, it does not work as far as I know and you need to go with the custom handlers.

1 Comment

basic config by itself was not doing it for me, but unsetting the existing handlers worked
5
endDate = '2015-07-24' logging.basicConfig(filename="win" + strftime("%Y%m%d", localtime()) + ".txt", level=logging.DEBUG,format='%(message)s') 

Comments

2

Use a Formatter.

1 Comment

I have set the python format FORMAT = "" logging.basicConfig(format=FORMAT) But still have INFO:root before
-1

Your really dont need to get into removing INFO word.. . (it shall really help you when your code would more messy and you would be using more stuff than just info like debugging exception etc)

If you want to compare you data with that data you can do something like skipping first 10 character (INFO:ROOT:) and then do whatever you feel like. Umm something like this:

f = open("my.log","a+") lines = f.readlines() for line in lines: if(line[10:] == my_output): do_whatever_you_feel_like() 

1 Comment

Yes,this way is work. But the compare is do by someone else,so he want me to give him a log without INFO:root. Other choice is to write a code more to delete the first 10 char

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.