Skip to content

Commit e2e4eed

Browse files
authored
Move logger into zeroconf.logger (#533)
1 parent 5100506 commit e2e4eed

2 files changed

Lines changed: 59 additions & 35 deletions

File tree

zeroconf/__init__.py

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import errno
2525
import ipaddress
2626
import itertools
27-
import logging
2827
import platform
2928
import select
3029
import socket
@@ -100,7 +99,7 @@
10099
NonUniqueNameException,
101100
ServiceNameAlreadyRegistered,
102101
)
103-
102+
from .logger import QuietLogger, log
104103

105104
__author__ = 'Paul Scott-Murphy, William McBrine'
106105
__maintainer__ = 'Jakub Stasiak <jakub@stasiak.at>'
@@ -129,12 +128,6 @@
129128
'''
130129
)
131130

132-
log = logging.getLogger(__name__)
133-
log.addHandler(logging.NullHandler())
134-
135-
if log.level == logging.NOTSET:
136-
log.setLevel(logging.WARN)
137-
138131

139132
int2byte = struct.Struct(">B").pack
140133

@@ -319,33 +312,6 @@ def instance_name_from_service_info(info: "ServiceInfo") -> str:
319312
# implementation classes
320313

321314

322-
class QuietLogger:
323-
_seen_logs = {} # type: Dict[str, Union[int, tuple]]
324-
325-
@classmethod
326-
def log_exception_warning(cls, *logger_data: Any) -> None:
327-
exc_info = sys.exc_info()
328-
exc_str = str(exc_info[1])
329-
if exc_str not in cls._seen_logs:
330-
# log at warning level the first time this is seen
331-
cls._seen_logs[exc_str] = exc_info
332-
logger = log.warning
333-
else:
334-
logger = log.debug
335-
logger(*(logger_data or ['Exception occurred']), exc_info=True)
336-
337-
@classmethod
338-
def log_warning_once(cls, *args: Any) -> None:
339-
msg_str = args[0]
340-
if msg_str not in cls._seen_logs:
341-
cls._seen_logs[msg_str] = 0
342-
logger = log.warning
343-
else:
344-
logger = log.debug
345-
cls._seen_logs[msg_str] = cast(int, cls._seen_logs[msg_str]) + 1
346-
logger(*args)
347-
348-
349315
class DNSEntry:
350316

351317
"""A DNS entry"""

zeroconf/logger.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
""" Multicast DNS Service Discovery for Python, v0.14-wmcbrine
2+
Copyright 2003 Paul Scott-Murphy, 2014 William McBrine
3+
4+
This module provides a framework for the use of DNS Service Discovery
5+
using IP multicast.
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20+
USA
21+
"""
22+
23+
import logging
24+
import sys
25+
from typing import Any, Dict, Union, cast
26+
27+
log = logging.getLogger(__name__.split('.')[0])
28+
log.addHandler(logging.NullHandler())
29+
30+
if log.level == logging.NOTSET:
31+
log.setLevel(logging.WARN)
32+
33+
34+
class QuietLogger:
35+
_seen_logs = {} # type: Dict[str, Union[int, tuple]]
36+
37+
@classmethod
38+
def log_exception_warning(cls, *logger_data: Any) -> None:
39+
exc_info = sys.exc_info()
40+
exc_str = str(exc_info[1])
41+
if exc_str not in cls._seen_logs:
42+
# log at warning level the first time this is seen
43+
cls._seen_logs[exc_str] = exc_info
44+
logger = log.warning
45+
else:
46+
logger = log.debug
47+
logger(*(logger_data or ['Exception occurred']), exc_info=True)
48+
49+
@classmethod
50+
def log_warning_once(cls, *args: Any) -> None:
51+
msg_str = args[0]
52+
if msg_str not in cls._seen_logs:
53+
cls._seen_logs[msg_str] = 0
54+
logger = log.warning
55+
else:
56+
logger = log.debug
57+
cls._seen_logs[msg_str] = cast(int, cls._seen_logs[msg_str]) + 1
58+
logger(*args)

0 commit comments

Comments
 (0)