Skip to content

Commit 93e07e7

Browse files
committed
Add cli logging example
1 parent 75683b0 commit 93e07e7

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

Diff for: source-code/command-line-arguments/Fire/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ applications with a command line interface.
66
## What is it?
77

88
1. `calculator.py`/`calculations.py`: Calculator application that
9-
implements addition and multiplication.
9+
implements addition, substraction, multiplication and division.
1010
1. `sayer.py`: Illustration of grouped commands.
11+
1. `cl_logger.py`/`log_management.py`/`log_operations.py`: logging
12+
from the command line example.

Diff for: source-code/command-line-arguments/Fire/cl_logger.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env python
2+
3+
import fire
4+
import log_operations
5+
6+
7+
if __name__ == '__main__':
8+
fire.Fire(log_operations)
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import re
2+
3+
4+
def create(file_name):
5+
with open('.cl_logger.txt', 'w') as file:
6+
print(f'file = {file_name}', file=file)
7+
8+
def _get_log_file():
9+
with open('.cl_logger.txt', 'r') as file:
10+
for line in file:
11+
if match := re.match('file = (.+)', line):
12+
return match[1]
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from datetime import datetime as _datetime
2+
from log_management import _get_log_file, create
3+
4+
5+
def _log(level, msg):
6+
time = _datetime.strftime(_datetime.now(), '%Y-%d-%m %H:%M:%S')
7+
with open(_get_log_file(), 'a') as file:
8+
print(f'{time} [{level}]: {msg}', file=file)
9+
10+
def error(msg):
11+
_log('error', msg)
12+
13+
def warning(msg):
14+
_log('warning', msg)
15+
16+
def info(msg):
17+
_log('info', msg)

0 commit comments

Comments
 (0)