1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
18 from aria.logger import (create_logger,
19 create_console_log_handler,
20 create_file_log_handler,
21 _default_file_formatter,
23 _DefaultConsoleFormat)
26 def test_create_logger():
28 logger = create_logger()
29 assert logger.name == 'aria'
30 assert len(logger.handlers) == 0
31 assert logger.level == logging.DEBUG
33 custom_logger = logging.getLogger('custom_logger')
34 handlers = [logging.FileHandler, logging.StreamHandler]
35 logger = create_logger(logger=custom_logger, handlers=handlers, level=logging.INFO)
36 assert custom_logger.name == 'custom_logger'
37 assert logger.handlers == handlers
38 assert logger.level == logging.INFO
41 def test_create_console_log_handler(capsys):
43 debug_test_string = 'debug_create_console_test_string'
44 info_test_string = 'info_create_console_test_string'
47 handler = create_console_log_handler()
48 assert isinstance(handler, logging.StreamHandler)
49 assert isinstance(handler.formatter, _DefaultConsoleFormat)
50 assert handler.level == logging.DEBUG
52 logger = create_logger(handlers=[handler])
54 logger.info(info_test_string)
55 logger.debug(debug_test_string)
56 _, err = capsys.readouterr()
58 assert '[DEBUG]> {test_string}'.format(test_string=debug_test_string) in err
59 assert err.count(info_test_string) == 1
62 custom_handler = create_console_log_handler(level=logging.INFO, formatter=logging.Formatter())
63 assert isinstance(custom_handler.formatter, logging.Formatter)
64 assert custom_handler.level == logging.INFO
66 logger = create_logger(handlers=[custom_handler])
68 logger.info(info_test_string)
69 _, err = capsys.readouterr()
71 assert err.count(info_test_string) == 1
74 def test_create_file_log_handler(tmpdir):
76 test_string = 'create_file_log_test_string'
78 debug_log = tmpdir.join('debug.log')
79 handler = create_file_log_handler(file_path=str(debug_log))
80 assert handler.baseFilename == str(debug_log)
81 assert handler.maxBytes == 5 * 1000 * 1024
82 assert handler.backupCount == 10
83 assert handler.stream is None
84 assert handler.level == logging.DEBUG
85 assert handler.formatter == _default_file_formatter
87 logger = create_logger(handlers=[handler])
88 logger.debug(test_string)
89 assert test_string in debug_log.read()
91 info_log = tmpdir.join('info.log')
92 handler = create_file_log_handler(
93 file_path=str(info_log),
97 formatter=logging.Formatter()
99 assert handler.baseFilename == str(info_log)
100 assert handler.level == logging.INFO
101 assert handler.maxBytes == 1000
102 assert handler.backupCount == 2
103 assert isinstance(handler.formatter, logging.Formatter)
105 logger = create_logger(handlers=[handler])
106 logger.info(test_string)
107 assert test_string in info_log.read()
110 def test_loggermixin(capsys):
112 test_string = 'loggermixing_test_string'
114 logger = create_logger(handlers=[create_console_log_handler()])
116 custom_class = type('CustomClass', (LoggerMixin,), {}).with_logger()
117 custom_class.logger.debug(test_string)
119 _, err = capsys.readouterr()
120 assert test_string in err
122 for handler in logger.handlers:
123 logger.removeHandler(handler)
125 # TODO: figure out what up with pickle
126 # class_pickled = pickle.dumps(custom_class)
127 # class_unpickled = pickle.loads(class_pickled)
129 # assert vars(class_unpickled) == vars(custom_class)