Passed
Pull Request — master (#21)
by
unknown
01:34
created

_gw_logging_cfg()   B

Complexity

Conditions 1

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
c 1
b 0
f 1
dl 0
loc 39
rs 8.8571
1
import os
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
import sys
3
4
5
def _gw_logging_cfg(log_file):
6
    return {
7
        'version': 1,
8
        'disable_existing_loggers': False,
9
        'formatters': {
10
            'standard': {
11
                'format': "%(asctime)s - %(levelname)-5s - %(message)s"
12
            },
13
            'debug': {
14
                'format': "%(asctime)s - %(levelname)-5s - %(name)-40s - %(message)-80s - %(module)s:%("
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (104/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
15
                          "funcName)s(%(lineno)s)"
16
            },
17
        },
18
        'handlers': {
19
            'console_stdout': {
20
                'formatter': 'standard',
21
                'class': 'logging.StreamHandler',
22
                'stream': sys.stdout,
23
                'level': 'INFO'
24
            },
25
            'file': {
26
                "class": "logging.handlers.RotatingFileHandler",
27
                "formatter": "debug",
28
                "filename": log_file,
29
                "maxBytes": 1024000,
30
                "backupCount": 3,
31
                'level': 'INFO'
32
            },
33
        },
34
        'loggers': {
35
            '': {
36
                'handlers': ['console_stdout'],
37
                'level': 'INFO',
38
                'propagate': False
39
            },
40
            'groundwork': {
41
                'handlers': ['console_stdout', 'file'],
42
                'level': 'INFO',
43
                'propagate': False
44
            },
45
        }
46
    }
47
48
49 View Code Duplication
def test_logging_file(basicApp, tmpdir):
0 ignored issues
show
Coding Style Naming introduced by
The name basicApp does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
50
    log_file = os.path.join(str(tmpdir), "test.log")
51
    app = basicApp
52
    # set up logging in the config, with log level INFO
53
    app.config.set('GROUNDWORK_LOGGING', _gw_logging_cfg(log_file))
54
    app._configure_logging(app.config.get("GROUNDWORK_LOGGING"))
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _configure_logging was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
55
56
    debug_message = "This is a test debug message."
57
    info_message = "This is a test info message."
58
    app.log.debug(debug_message)
59
    app.log.info(info_message)
60
61
    # verify the contents of the log file
62
    with open(log_file) as lf:
0 ignored issues
show
Coding Style Naming introduced by
The name lf does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
63
        log = lf.read()
64
    # at log level INFO, the DEBUG message should not be there
65
    assert log.find(debug_message) == -1
66
    # the INFO message should be there
67
    assert log.find(info_message) > 0
68
69
70 View Code Duplication
def test_logging_console(basicApp, tmpdir, capsys):
0 ignored issues
show
Coding Style Naming introduced by
The name basicApp does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
71
    log_file = os.path.join(str(tmpdir), "test.log")
72
    app = basicApp
73
    # set up logging in the config, with log level INFO
74
    app.config.set('GROUNDWORK_LOGGING', _gw_logging_cfg(log_file))
75
    app._configure_logging(app.config.get("GROUNDWORK_LOGGING"))
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _configure_logging was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
76
77
    debug_message = "This is a test debug message."
78
    info_message = "This is a test info message."
79
    app.log.debug(debug_message)
80
    app.log.info(info_message)
81
82
    out, err = capsys.readouterr()
0 ignored issues
show
Unused Code introduced by
The variable err seems to be unused.
Loading history...
83
    # at log level INFO, the DEBUG message should not be there
84
    assert out.find(debug_message) == -1
85
    # the INFO message should be there
86
    assert out.find(info_message) > 0
87