Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #57734
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'subject:not': 0.03; 'handler': 0.05; 'root': 0.05; 'args': 0.07; 'parser': 0.07; 'subject:file': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'main()': 0.09; 'parsing': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'def': 0.12; 'commandline': 0.16; 'logger': 0.16; 'main():': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:Logging': 0.16; 'variants': 0.16; 'weird': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'bit': 0.19; 'result.': 0.19; 'import': 0.22; 'shell': 0.22; 'this?': 0.23; 'header:User- Agent:1': 0.23; 'skip:l 30': 0.24; 'logging': 0.26; 'header:X -Complaints-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'skip:p 30': 0.29; 'sets': 0.30; 'code': 0.31; 'lines': 0.31; 'file': 0.32; 'there.': 0.32; 'probably': 0.32; 'critical': 0.32; 'figure': 0.32; 'interface': 0.32; "can't": 0.35; 'except': 0.35; 'but': 0.35; 'really': 0.36; 'crazy': 0.36; 'doing': 0.36; 'should': 0.36; 'application': 0.37; 'configured': 0.38; 'to:addr :python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:l 50': 0.60; 'show': 0.63; 'name': 0.63; 'skip:r 30': 0.69; 'message")': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Logging to file and not to console |
| Date | Sun, 27 Oct 2013 11:09:08 +0100 |
| Organization | None |
| References | <20131027020458.2a314eb9@hanez.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p5084b9e1.dip0.t-ipconnect.de |
| User-Agent | KNode/4.7.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1644.1382868539.18130.python-list@python.org> (permalink) |
| Lines | 70 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1382868540 news.xs4all.nl 15877 [2001:888:2000:d::a6]:34390 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:57734 |
Show key headers only | View raw
Johannes Findeisen wrote:
> Hi all,
>
> I am going crazy with logging. I have an application which sets up
> logging after parsing the args in the main() funktion. It needs to be
> setup after parsing the args because I can set the loglevel via
> commandline flags.
>
> I have tried many variants on how to do that but every time with an
> weird result.
>
> What I want is logging in from all libs and really understand that doing
> this should be enough there:
>
> from logging import getLogger
> logger = getLogger(__name__)
>
> But, I need to setup the logger in the main() function to log only to a
> file and not to console because my application has an own shell
> interface which should not be spammed with log messages - never a
> message should show up there.
>
> I think it should be only some few lines of code but I can't figure
> that out. The logger should be configured to have a max file size and
> rotate logfiles.
>
> Can someone help me with this?
import logging
import logging.handlers
logger = logging.getLogger(__name__)
def levelnames():
try:
names = logging._nameToLevel
except AttributeError:
names = (name for name in logging._levelNames if isinstance(name,
str))
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--logging-file", default="tmp.log")
parser.add_argument("--logging-level", choices=levelnames(),
default="INFO")
args = parser.parse_args()
root = logging.getLogger()
formatter = logging.Formatter(logging.BASIC_FORMAT)
handler = logging.handlers.RotatingFileHandler(args.logging_file,
maxBytes=100, backupCount=3)
handler.setFormatter(formatter)
root.addHandler(handler)
root.setLevel(args.logging_level)
logger.info("your info")
logger.warn("your warning")
logger.critical("your critical message")
if __name__ == "__main__":
main()
Does this do what you want?
(maxBytes is probably a bit low)
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Logging to file and not to console Peter Otten <__peter__@web.de> - 2013-10-27 11:09 +0100
csiph-web