summaryrefslogtreecommitdiffstats
path: root/azure/aria/aria-extension-cloudify/src/aria/tests/test_logger.py
blob: d6999fddc478b61597eaeeb0c858879a8f6424e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

from aria.logger import (create_logger,
                         create_console_log_handler,
                         create_file_log_handler,
                         _default_file_formatter,
                         LoggerMixin,
                         _DefaultConsoleFormat)


def test_create_logger():

    logger = create_logger()
    assert logger.name == 'aria'
    assert len(logger.handlers) == 0
    assert logger.level == logging.DEBUG

    custom_logger = logging.getLogger('custom_logger')
    handlers = [logging.FileHandler, logging.StreamHandler]
    logger = create_logger(logger=custom_logger, handlers=handlers, level=logging.INFO)
    assert custom_logger.name == 'custom_logger'
    assert logger.handlers == handlers
    assert logger.level == logging.INFO


def test_create_console_log_handler(capsys):

    debug_test_string = 'debug_create_console_test_string'
    info_test_string = 'info_create_console_test_string'

    # Default handler
    handler = create_console_log_handler()
    assert isinstance(handler, logging.StreamHandler)
    assert isinstance(handler.formatter, _DefaultConsoleFormat)
    assert handler.level == logging.DEBUG

    logger = create_logger(handlers=[handler])

    logger.info(info_test_string)
    logger.debug(debug_test_string)
    _, err = capsys.readouterr()

    assert '[DEBUG]> {test_string}'.format(test_string=debug_test_string) in err
    assert err.count(info_test_string) == 1

    # Custom handler
    custom_handler = create_console_log_handler(level=logging.INFO, formatter=logging.Formatter())
    assert isinstance(custom_handler.formatter, logging.Formatter)
    assert custom_handler.level == logging.INFO

    logger = create_logger(handlers=[custom_handler])

    logger.info(info_test_string)
    _, err = capsys.readouterr()

    assert err.count(info_test_string) == 1


def test_create_file_log_handler(tmpdir):

    test_string = 'create_file_log_test_string'

    debug_log = tmpdir.join('debug.log')
    handler = create_file_log_handler(file_path=str(debug_log))
    assert handler.baseFilename == str(debug_log)
    assert handler.maxBytes == 5 * 1000 * 1024
    assert handler.backupCount == 10
    assert handler.stream is None
    assert handler.level == logging.DEBUG
    assert handler.formatter == _default_file_formatter

    logger = create_logger(handlers=[handler])
    logger.debug(test_string)
    assert test_string in debug_log.read()

    info_log = tmpdir.join('info.log')
    handler = create_file_log_handler(
        file_path=str(info_log),
        level=logging.INFO,
        max_bytes=1000,
        backup_count=2,
        formatter=logging.Formatter()
    )
    assert handler.baseFilename == str(info_log)
    assert handler.level == logging.INFO
    assert handler.maxBytes == 1000
    assert handler.backupCount == 2
    assert isinstance(handler.formatter, logging.Formatter)

    logger = create_logger(handlers=[handler])
    logger.info(test_string)
    assert test_string in info_log.read()


def test_loggermixin(capsys):

    test_string = 'loggermixing_test_string'

    logger = create_logger(handlers=[create_console_log_handler()])

    custom_class = type('CustomClass', (LoggerMixin,), {}).with_logger()
    custom_class.logger.debug(test_string)

    _, err = capsys.readouterr()
    assert test_string in err

    for handler in logger.handlers:
        logger.removeHandler(handler)

    # TODO: figure out what up with pickle
    # class_pickled = pickle.dumps(custom_class)
    # class_unpickled = pickle.loads(class_pickled)
    #
    # assert vars(class_unpickled) == vars(custom_class)