looptools.log   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 82.05 %

Importance

Changes 0
Metric Value
wmc 5
eloc 28
dl 32
loc 39
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A LogOutput.logger() 4 4 2
A LogOutput.__init__() 10 10 1
A LogOutput.setup_custom_logger() 15 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import os
2
import sys
3
from datetime import datetime
4
import logging
5
6
7 View Code Duplication
class LogOutput:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
8
    def __init__(self, save_directory, name, enable_printing=True):
9
        """
10
        Save logged output to a text file
11
        :param save_directory: Directory to save log files to
12
        :param name: Name of process being logged
13
        """
14
        self.filename = str(save_directory) + '/_logs/' + str(datetime.now().strftime("%Y-%m-%d")) + '.txt'
15
        self.name = name
16
        self.log_file = self.setup_custom_logger()
17
        self.enable_printing = enable_printing
18
19
    def logger(self, msg):
20
        if self.enable_printing:
21
            print(str('\n' + msg))
22
        self.log_file.info(msg)
23
24
    def setup_custom_logger(self):
25
        formatter = logging.Formatter(fmt='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p: ')
26
        if os.path.exists(self.filename):
27
            append_write = 'a'    # append if already exists
28
        else:
29
            append_write = 'w'    # make a new file if not
30
        handler = logging.FileHandler(self.filename, mode=append_write)
31
        handler.setFormatter(formatter)
32
        screen_handler = logging.StreamHandler(stream=sys.stdout)
33
        screen_handler.setFormatter(formatter)
34
        logger = logging.getLogger(self.name)
35
        logger.setLevel(logging.DEBUG)
36
        logger.addHandler(handler)
37
        # logger.addHandler(screen_handler)
38
        return logger
39