Logging decorator
Mon Jan 31 2022 02:23:59 GMT+0000 (Coordinated Universal Time)
Saved by
@aguest
#python
#decorator
from datetime import datetime
def logger(func):
def wrapper(*args, **kwargs):
print('_' * 25)
print(f'Run on: {datetime.today().strftime("%Y-%m-%d %H:%M:%S")}')
print(f'Running function: {func.__name__}')
func(*args, **kwargs)
print('_' * 25)
return wrapper
content_copyCOPY
## Usage example
```python
@logger
def shutdown():
print('System shutdown')
@logger
def restart():
print('System restarts')
shutdown()
restart()
```
## Output
_________________________
Run on: 2021-10-15 23:21:57
Running function: shutdown
System shutdown
_________________________
Run on: 2021-10-15 23:21:57
Running function: restart
System restarts
_________________________
Comments