[Tutor] Please help disentangle my program logic

Robert Alexander gogonegro at gmail.com
Mon Jan 6 06:19:12 EST 2020


As per Mats suggestion I am wrangling with the logging module.
Almost there but I don’t understand what I’m doing wrong since I wish the
log
to go only into a specified log file but my current code spits out TWO
different
Log lines:

A) an unwanted and strangely formatted log on the screen with lines such as
INFO:__main__:DL:211.2;UL:29.8;PL:0
WARNING:__main__:DL:125.8;UL:29.6;PL:0

B) The properly formatted log lines in the specified log file. Here the
format
2020-01-06 12:07:05;INFO;DL:211.2;UL:29.8;PL:0
2020-01-06 12:08:01;WARNING;DL:125.8;UL:29.6;PL:0

Here’s the code:

import logging
….
#  setup logging
log_file_handler = logging.FileHandler('adsllog.log')
log_formatter = logging.Formatter("%(asctime)s;%(levelname)s;%(message)s",
"%Y-%m-%d %H:%M:%S")
log_file_handler.setFormatter(log_formatter)

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# add file handler to logger
logger.addHandler(log_file_handler)
…
speedtest_values_string = (
                f"DL:{down_speed:3.1f};UL:{up_speed:2.1f};PL:{packet_loss}"
            )
if down_speed < TD or up_speed < TU or packet_loss > TL:
                logger.warning(f"{speedtest_values_string}")
            else:
                logger.info(f"{speedtest_values_string}”)

In the above code TD,TU,TL are numeric thresholds and down_speed, up_speed
and packet_loss are my measurements of my ADSL line.

Thank you for any clarification.

Robert

On 5 January 2020 at 19:34:51, Mats Wichmann (mats at wichmann.us) wrote:

On 1/5/20 11:23 AM, Robert Alexander wrote:
> Thank you Mats. Very interesting observation.
>
> May you kindly expand on why you are suggesting this?
>
> In my opinion specifying a log name at runtime gives me more flexibility
> but I might be not considering some other important reason.

Not sure I'm answering the right question here, but...

You can still specify the logging location on the command line by
accepting an argument, and having a default in the program if it's not
on the command line.

The logigng module gives you the flexibility to log to different places,
even - you can put every reading in the log, and important events (like
Internet connection is down) somewhere else - your console, sending by
email (I'm not sure that's directly supported, but wouldn't be hard to
set up), by assigning different log levels to different types of events.

>
> Ciao
>
> PS Etiquette question please: Is it better to reply to both the sender
and
> the list or the list only?
>
>
> On 5 January 2020 at 19:18:30, Mats Wichmann (mats at wichmann.us) wrote:
>

> quick reaction: look into the logging module to control how, where and
> what to log. No need to reinvent the wheel.
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list