Skip to content

Commit

Permalink
Merge pull request #6 from fosslight/dd-jy-patch-1
Browse files Browse the repository at this point in the history
Add options to change stream and file log level
  • Loading branch information
dd-jy authored May 13, 2021
2 parents 347460e + fcc74c7 commit f3292c6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,19 @@ Three modules can be called. Please refer to each file for detailed calling meth

### 1. Setup logger (tests/test_log.py)
```
from fosslight_util._set_log import init_log
from fosslight_util.set_log import init_log
# 1st param : log file path
# 2nd param : create file (True/False)
# 3rd param : stream log level
# 4th param : file log level
# =>log level(CRITICAL:50, ERROR:40, WARNING:30, INFO:20, DEBUG:10, NOTSET:0)
def test():
logger = init_log("test_result/log_file1.txt")
logger = init_log("test_result/log_file1.txt", True, 30, 20)
logger.warning("TESTING - Print log")
```


### 2. Write csv and excel files (tests/test_excel.py)
```
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
if __name__ == "__main__":
setup(
name='fosslight_util',
version='1.0.4',
version='1.0.5',
package_dir={"": "src"},
packages=find_packages(where='src'),
description='FOSSLight Util',
Expand Down
32 changes: 17 additions & 15 deletions src/fosslight_util/set_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,30 @@
from . import constant as constant


def init_log(log_file, create_file=True):
def init_log(log_file, create_file=True, stream_log_level=logging.WARN, file_log_level=logging.INFO):

logger = logging.getLogger(constant.LOGGER_NAME)

if not logger.hasHandlers():
log_level = logging.WARNING
formatter = logging.Formatter('%(message)s')

logger.setLevel(logging.DEBUG)
log_dir = os.path.dirname(log_file)
Path(log_dir).mkdir(parents=True, exist_ok=True)

if create_file:
file_hanlder = logging.FileHandler(log_file)
file_hanlder.setLevel(log_level)
file_hanlder.setFormatter(formatter)
file_hanlder.propagate = False
logger.addHandler(file_hanlder)

console = logging.StreamHandler()
console.setLevel(log_level)
console.setFormatter(formatter)
console.propagate = False
logger.addHandler(console)

file_handlder = logging.FileHandler(log_file)
file_handlder.setLevel(file_log_level)
file_formatter = logging.Formatter('[%(levelname)7s] %(message)s')
file_handlder.setFormatter(file_formatter)
file_handlder.propagate = False
logger.addHandler(file_handlder)

console_handler = logging.StreamHandler()
console_handler.setLevel(stream_log_level)
console_formatter = logging.Formatter('%(message)s')
console_handler.setFormatter(console_formatter)
console_handler.propagate = False
logger.addHandler(console_handler)

logger.propagate = False

Expand Down

0 comments on commit f3292c6

Please sign in to comment.