Skip to content

Commit 4025164

Browse files
kmykkyuridenamida
authored andcommitted
root logger を使うのを止める (kyuridenamida#142)
* Stop using the root logger of logging module * Fix mistakes in updating logging
1 parent 8d9e7a4 commit 4025164

File tree

10 files changed

+72
-57
lines changed

10 files changed

+72
-57
lines changed

atcodertools/client/atcoder.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import getpass
2-
import logging
32
import os
43
import re
54
import warnings
@@ -11,6 +10,7 @@
1110

1211
from atcodertools.client.models.submission import Submission
1312
from atcodertools.common.language import Language
13+
from atcodertools.common.logging import logger
1414
from atcodertools.fileutils.artifacts_cache import get_cache_file_path
1515
from atcodertools.client.models.contest import Contest
1616
from atcodertools.client.models.problem import Problem
@@ -28,7 +28,7 @@ def save_cookie(session: requests.Session, cookie_path: Optional[str] = None):
2828
cookie_path = cookie_path or default_cookie_path
2929
os.makedirs(os.path.dirname(cookie_path), exist_ok=True)
3030
session.cookies.save()
31-
logging.info("Saved session into {}".format(os.path.abspath(cookie_path)))
31+
logger.info("Saved session into {}".format(os.path.abspath(cookie_path)))
3232
os.chmod(cookie_path, 0o600)
3333

3434

@@ -37,7 +37,7 @@ def load_cookie_to(session: requests.Session, cookie_path: Optional[str] = None)
3737
session.cookies = LWPCookieJar(cookie_path)
3838
if os.path.exists(cookie_path):
3939
session.cookies.load()
40-
logging.info(
40+
logger.info(
4141
"Loaded session from {}".format(os.path.abspath(cookie_path)))
4242
return True
4343
return False
@@ -80,9 +80,9 @@ def login(self,
8080
if use_local_session_cache:
8181
load_cookie_to(self._session)
8282
if self.check_logging_in():
83-
logging.info(
83+
logger.info(
8484
"Successfully Logged in using the previous session cache.")
85-
logging.info(
85+
logger.info(
8686
"If you'd like to invalidate the cache, delete {}.".format(default_cookie_path))
8787

8888
return

atcodertools/common/logging.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import logging
2+
3+
logger = logging.getLogger(__name__)
4+
logger.setLevel(logging.INFO)
5+
handler = logging.StreamHandler()
6+
formatter = logging.Formatter("%(asctime)s %(levelname)s: %(message)s")
7+
handler.setFormatter(formatter)
8+
logger.addHandler(handler)

atcodertools/constprediction/constants_prediction.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import logging
21
import re
32
from typing import Tuple, Optional
43

54
from bs4 import BeautifulSoup
65

76
from atcodertools.constprediction.models.problem_constant_set import ProblemConstantSet
87
from atcodertools.client.models.problem_content import ProblemContent, InputFormatDetectionError, SampleDetectionError
8+
from atcodertools.common.logging import logger
99

1010

1111
class YesNoPredictionFailedError(Exception):
@@ -92,8 +92,8 @@ def predict_constants(html: str) -> ProblemConstantSet:
9292
try:
9393
mod = predict_modulo(html)
9494
except MultipleModCandidatesError as e:
95-
logging.warning("Modulo prediction failed -- "
96-
"two or more candidates {} are detected as modulo values".format(e.cands))
95+
logger.warning("Modulo prediction failed -- "
96+
"two or more candidates {} are detected as modulo values".format(e.cands))
9797
mod = None
9898

9999
return ProblemConstantSet(mod=mod, yes_str=yes_str, no_str=no_str)

atcodertools/tools/codegen.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/python3
22
import argparse
3-
import logging
43
import os
54
import posixpath
65
import re
@@ -16,6 +15,7 @@
1615
from atcodertools.codegen.code_style_config import DEFAULT_WORKSPACE_DIR_PATH
1716
from atcodertools.codegen.models.code_gen_args import CodeGenArgs
1817
from atcodertools.common.language import ALL_LANGUAGES, CPP
18+
from atcodertools.common.logging import logger
1919
from atcodertools.config.config import Config
2020
from atcodertools.constprediction.constants_prediction import predict_constants
2121
from atcodertools.fmtprediction.models.format_prediction_result import FormatPredictionResult
@@ -67,13 +67,13 @@ def generate_code(atcoder_client: AtCoderClient,
6767
lang = config.code_style_config.lang
6868

6969
def emit_error(text):
70-
logging.error(with_color(text, Fore.RED))
70+
logger.error(with_color(text, Fore.RED))
7171

7272
def emit_warning(text):
73-
logging.warning(text)
73+
logger.warning(text)
7474

7575
def emit_info(text):
76-
logging.info(text)
76+
logger.info(text)
7777

7878
emit_info('{} is used for template'.format(template_code_path))
7979

@@ -164,13 +164,13 @@ def main(prog, args, output_file=sys.stdout):
164164
try:
165165
client.login(
166166
save_session_cache=not config.etc_config.save_no_session_cache)
167-
logging.info("Login successful.")
167+
logger.info("Login successful.")
168168
except LoginError:
169-
logging.error(
169+
logger.error(
170170
"Failed to login (maybe due to wrong username/password combination?)")
171171
sys.exit(-1)
172172
else:
173-
logging.info("Downloading data without login.")
173+
logger.info("Downloading data without login.")
174174

175175
generate_code(client,
176176
args.url,

atcodertools/tools/envgen.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/python3
22
import argparse
3-
import logging
43
import os
54
import shutil
65
import sys
@@ -18,6 +17,7 @@
1817
from atcodertools.codegen.code_style_config import DEFAULT_WORKSPACE_DIR_PATH
1918
from atcodertools.codegen.models.code_gen_args import CodeGenArgs
2019
from atcodertools.common.language import ALL_LANGUAGES, CPP
20+
from atcodertools.common.logging import logger
2121
from atcodertools.config.config import Config
2222
from atcodertools.constprediction.constants_prediction import predict_constants
2323
from atcodertools.fileutils.create_contest_file import create_examples, \
@@ -29,9 +29,6 @@
2929
from atcodertools.tools.models.metadata import Metadata
3030
from atcodertools.tools.utils import with_color
3131

32-
fmt = "%(asctime)s %(levelname)s: %(message)s"
33-
logging.basicConfig(level=logging.INFO, format=fmt)
34-
3532

3633
class BannedFileDetectedError(Exception):
3734
pass
@@ -60,13 +57,13 @@ def prepare_procedure(atcoder_client: AtCoderClient,
6057
pid)
6158

6259
def emit_error(text):
63-
logging.error(with_color("Problem {}: {}".format(pid, text), Fore.RED))
60+
logger.error(with_color("Problem {}: {}".format(pid, text), Fore.RED))
6461

6562
def emit_warning(text):
66-
logging.warning("Problem {}: {}".format(pid, text))
63+
logger.warning("Problem {}: {}".format(pid, text))
6764

6865
def emit_info(text):
69-
logging.info("Problem {}: {}".format(pid, text))
66+
logger.info("Problem {}: {}".format(pid, text))
7067

7168
emit_info('{} is used for template'.format(template_code_path))
7269

@@ -169,7 +166,7 @@ def prepare_contest(atcoder_client: AtCoderClient,
169166
if problem_list:
170167
break
171168
sleep(retry_duration)
172-
logging.warning(
169+
logger.warning(
173170
"Failed to fetch. Will retry in {} seconds".format(retry_duration))
174171

175172
tasks = [(atcoder_client,
@@ -194,8 +191,8 @@ def prepare_contest(atcoder_client: AtCoderClient,
194191
if config.postprocess_config.exec_cmd_on_contest_dir is not None:
195192
contest_dir_path = os.path.join(
196193
config.code_style_config.workspace_dir, contest_id)
197-
logging.info(_message_on_execution(contest_dir_path,
198-
config.postprocess_config.exec_cmd_on_contest_dir))
194+
logger.info(_message_on_execution(contest_dir_path,
195+
config.postprocess_config.exec_cmd_on_contest_dir))
199196
config.postprocess_config.execute_on_contest_dir(
200197
contest_dir_path)
201198

@@ -206,7 +203,7 @@ def prepare_contest(atcoder_client: AtCoderClient,
206203

207204
def get_config(args: argparse.Namespace) -> Config:
208205
def _load(path: str) -> Config:
209-
logging.info("Going to load {} as config".format(path))
206+
logger.info("Going to load {} as config".format(path))
210207
with open(path, 'r') as f:
211208
return Config.load(f, args)
212209

@@ -277,9 +274,9 @@ def main(prog, args):
277274
args = parser.parse_args(args)
278275

279276
if args.replacement is not None:
280-
logging.error(with_color("Sorry! --replacement argument no longer exists"
281-
" and you can only use --template."
282-
" See the official document for details.", Fore.LIGHTRED_EX))
277+
logger.error(with_color("Sorry! --replacement argument no longer exists"
278+
" and you can only use --template."
279+
" See the official document for details.", Fore.LIGHTRED_EX))
283280
raise DeletedFunctionalityError
284281

285282
config = get_config(args)
@@ -296,13 +293,13 @@ def main(prog, args):
296293
try:
297294
client.login(
298295
save_session_cache=not config.etc_config.save_no_session_cache)
299-
logging.info("Login successful.")
296+
logger.info("Login successful.")
300297
except LoginError:
301-
logging.error(
298+
logger.error(
302299
"Failed to login (maybe due to wrong username/password combination?)")
303300
sys.exit(-1)
304301
else:
305-
logging.info("Downloading data without login.")
302+
logger.info("Downloading data without login.")
306303

307304
prepare_contest(client,
308305
args.contest_id,

atcodertools/tools/submit.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/python3
22
import argparse
3-
import logging
43
import sys
54
import os
65

@@ -9,6 +8,7 @@
98

109
from atcodertools.client.atcoder import AtCoderClient, LoginError
1110
from atcodertools.tools import tester
11+
from atcodertools.common.logging import logger
1212

1313
from atcodertools.tools.models.metadata import Metadata
1414

@@ -58,7 +58,7 @@ def main(prog, args, credential_supplier=None, use_local_session_cache=True) ->
5858
try:
5959
metadata = Metadata.load_from(metadata_file)
6060
except IOError:
61-
logging.error(
61+
logger.error(
6262
"{0} is not found! You need {0} to use this submission functionality.".format(metadata_file))
6363
return False
6464

@@ -69,7 +69,7 @@ def main(prog, args, credential_supplier=None, use_local_session_cache=True) ->
6969
use_local_session_cache=use_local_session_cache,
7070
)
7171
except LoginError:
72-
logging.error("Login failed. Try again.")
72+
logger.error("Login failed. Try again.")
7373
return False
7474

7575
tester_args = []
@@ -85,19 +85,19 @@ def main(prog, args, credential_supplier=None, use_local_session_cache=True) ->
8585
if not args.unlock_safety:
8686
for submission in submissions:
8787
if submission.problem_id == metadata.problem.problem_id:
88-
logging.error(with_color("Cancel submitting because you already sent some code to the problem. Please "
89-
"specify -u to send the code. {}".format(
90-
metadata.problem.contest.get_submissions_url(submission)), Fore.LIGHTRED_EX))
88+
logger.error(with_color("Cancel submitting because you already sent some code to the problem. Please "
89+
"specify -u to send the code. {}".format(
90+
metadata.problem.contest.get_submissions_url(submission)), Fore.LIGHTRED_EX))
9191
return False
9292

9393
code_path = args.code or os.path.join(args.dir, metadata.code_filename)
9494
with open(code_path, 'r') as f:
9595
source = f.read()
96-
logging.info(
96+
logger.info(
9797
"Submitting {} as {}".format(code_path, metadata.lang.name))
9898
submission = client.submit_source_code(
9999
metadata.problem.contest, metadata.problem, metadata.lang, source)
100-
logging.info("{} {}".format(
100+
logger.info("{} {}".format(
101101
with_color("Done!", Fore.LIGHTGREEN_EX),
102102
metadata.problem.contest.get_submissions_url(submission)))
103103

atcodertools/tools/tester.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#!/usr/bin/python3
22
import argparse
33
import glob
4-
import logging
54
import os
65
import sys
76
from pathlib import Path
87
from typing import List, Tuple
98

109
from colorama import Fore
1110

11+
from atcodertools.common.logging import logger
1212
from atcodertools.executils.run_program import ExecResult, ExecStatus, run_program
1313
from atcodertools.tools.models.metadata import Metadata
1414
from atcodertools.tools.utils import with_color
@@ -45,7 +45,7 @@ def infer_exec_file(filenames):
4545

4646
exec_file = exec_files[0]
4747
if len(exec_files) >= 2:
48-
logging.warning("{0} {1}".format(
48+
logger.warning("{0} {1}".format(
4949
"There're multiple executable files. '{exec_file}' is selected.".format(
5050
exec_file=exec_file),
5151
"The candidates were {exec_files}.".format(exec_files=exec_files)))
@@ -140,7 +140,7 @@ def run_for_samples(exec_file: str, sample_pair_list: List[Tuple[str, str]], tim
140140

141141
def validate_sample_pair(in_sample_file, out_sample_file):
142142
if infer_case_num(in_sample_file) != infer_case_num(out_sample_file):
143-
logging.error(
143+
logger.error(
144144
'The file combination of {} and {} is wrong.'.format(
145145
in_sample_file,
146146
out_sample_file
@@ -177,7 +177,7 @@ def single_or_none(lst: List):
177177
def run_all_tests(exec_file, in_sample_file_list, out_sample_file_list, timeout_sec: int, knock_out: bool,
178178
skip_stderr_on_success: bool) -> bool:
179179
if len(in_sample_file_list) != len(out_sample_file_list):
180-
logging.error("{0}{1}{2}".format(
180+
logger.error("{0}{1}{2}".format(
181181
"The number of the sample inputs and outputs are different.\n",
182182
"# of sample inputs: {}\n".format(len(in_sample_file_list)),
183183
"# of sample outputs: {}\n".format(len(out_sample_file_list))))
@@ -218,7 +218,7 @@ def get_sample_patterns(metadata_file: str) -> Tuple[str, str]:
218218
metadata = Metadata.load_from(metadata_file)
219219
return metadata.sample_in_pattern, metadata.sample_out_pattern
220220
except IOError:
221-
logging.warning("{} is not found. Assume the example file name patterns are {} and {}".format(
221+
logger.warning("{} is not found. Assume the example file name patterns are {} and {}".format(
222222
metadata_file,
223223
DEFAULT_IN_EXAMPLE_PATTERN,
224224
DEFAULT_OUT_EXAMPLE_PATTERN)

tests/test_constpred.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import logging
21
import os
32
import tempfile
43
import unittest
4+
from logging import getLogger, DEBUG, Formatter, StreamHandler
55

66
from atcodertools.constprediction.constants_prediction import predict_constants, predict_modulo, \
77
MultipleModCandidatesError, predict_yes_no, YesNoPredictionFailedError
@@ -11,8 +11,12 @@
1111
os.path.dirname(os.path.abspath(__file__)),
1212
'./resources/test_constpred/answer.txt')
1313

14-
fmt = "%(asctime)s %(levelname)s: %(message)s"
15-
logging.basicConfig(level=logging.DEBUG, format=fmt)
14+
logger = getLogger(__name__)
15+
logger.setLevel(DEBUG)
16+
handler = StreamHandler()
17+
formatter = Formatter("%(asctime)s %(levelname)s: %(message)s")
18+
handler.setFormatter(formatter)
19+
logger.addHandler(handler)
1620

1721

1822
def _to_str(x):
@@ -39,7 +43,7 @@ def test_predict_constants(self):
3943
agc_html_paths = [path for path in sorted(
4044
os.listdir(self.test_dir)) if "agc" in path]
4145
for html_path, answer_line in zip(agc_html_paths, answers):
42-
logging.debug("Testing {}".format(html_path))
46+
logger.debug("Testing {}".format(html_path))
4347
constants = predict_constants(self._load(html_path))
4448
output_line = "{:40} [mod]{:10} [yes]{:10} [no]{:10}".format(html_path.split(".")[0],
4549
_to_str(

tests/test_envgen.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import logging
21
import os
32
import shutil
43
import tempfile
54
import unittest
65
from os.path import relpath
6+
from logging import getLogger
77

88
from atcodertools.client.atcoder import AtCoderClient
99
from atcodertools.codegen.code_style_config import CodeStyleConfig
1010
from atcodertools.config.config import Config
1111
from atcodertools.config.etc_config import EtcConfig
1212
from atcodertools.tools.envgen import prepare_contest, main
1313

14+
logger = getLogger(__name__)
15+
1416
RESOURCE_DIR = os.path.join(
1517
os.path.dirname(os.path.abspath(__file__)),
1618
"./resources/test_atc_env/")
@@ -33,7 +35,7 @@ def setUp(self):
3335

3436
def tearDown(self):
3537
shutil.rmtree(self.temp_dir)
36-
logging.info(self.temp_dir)
38+
logger.info(self.temp_dir)
3739

3840
def test_prepare_workspace(self):
3941
answer_data_dir_path = os.path.join(

0 commit comments

Comments
 (0)