Skip to content

Commit

Permalink
+ easy_logging, s hotkey
Browse files Browse the repository at this point in the history
  • Loading branch information
vikilpet committed Jul 16, 2020
1 parent c2881ad commit 911ced8
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 152 deletions.
125 changes: 125 additions & 0 deletions easy_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import os
from atexit import register as ae_register
from datetime import timedelta
from datetime import datetime as dt

class EasyLogging:
''' Logging to console and optionally to a disk.
Attributes:
level - logging level. Default is 10 ('INFO').
If level == 0 then show debug messages.
If level == 20 then show only error messages.
Default levels:
'DEBUG' : 0
'INFO' : 10
'ERROR' : 20
time_format:str='%Y.%m.%d %H:%M:%S'
file_name_format:str='%Y-%m-%d.log'
directory:str - write log files to this folder.
If not specified - no logging to a file.
levels:dict - provide your own levels.
Format: {'level name 1': level_num1
, 'level name 2': level_num2}
add_levels - just add this level(s) to the default
levels dictionary. Format:
[('my_level_1', 15), ('my_level_2', 30)]
line_max_len:int - if specified cut the message
to that length (log datestamp not included).
sep:str - separator between columns.
'''
def __init__(
s
, level:int=10
, directory:str=None
, file_name_format:str='%Y-%m-%d.log'
, time_format:str='%Y.%m.%d %H:%M:%S'
, add_levels:list=None
, levels:dict={
'DEBUG' : 0
, 'INFO' : 10
, 'ERROR' : 20
}
, line_max_len:int=None
, sep:str = ' : '
):
s.levels = levels
if add_levels:
if type(add_levels[0]) in [list, tuple]:
for l, n in add_levels:
s.levels[l] = n
else:
s.levels[add_levels[0]] = add_levels[1]
s.level = level
s.time_format = time_format
for key, value in levels.items():
setattr(
EasyLogging
, key.lower()
, lambda s, *strings, l=key: s._log(*strings, lvl=l)
)
setattr(EasyLogging, key.upper(), value)
s.lvl_pad = max(*map(len, levels))
s.sep = sep
s.line_max_len = line_max_len
s.filed = None
if directory:
if not os.path.exists(directory): os.mkdir(directory)
s.file_name_format = file_name_format
s.directory = directory
s.file_name = dt.now().strftime(
file_name_format)
s.filed = open(
os.path.join(s.directory, s.file_name)
, 'ta+'
)
ae_register(s._cleanup)

def _log(s, *strings, lvl:str='DEBUG'):
'Log to console and optionally to disk'
if s.levels.get(lvl, s.level) < s.level: return
msg = s.sep.join(map(str, strings))
if s.line_max_len: msg = msg[:s.line_max_len]
t = dt.now().strftime(s.time_format)
msg = f'{t}{s.sep}{lvl:{s.lvl_pad}}{s.sep}{msg}'
print(msg)
if not s.filed: return
s._write_to_file(msg)

def _cleanup(s):
if not s.filed: return
s._log('cleanup', lvl='DEBUG')
s.filed.close()

def _write_to_file(s, msg):
fn = dt.now().strftime(s.file_name_format)
if fn != s.file_name:
s.file_name = fn
s.filed.close()
s.filed = open(
os.path.join(s.directory, s.file_name)
, 'ta+'
)
s.filed.write(msg + '\n')
s.filed.flush()

def __getattr__(s, name):
def method(*args, **kwargs):
s._log(lvl=name.upper(), *args, **kwargs)
return method

if __name__ == '__main__':
log = EasyLogging(
line_max_len=20
)
log.debug('This msg will be omitted with default logging level')
log.info('Info message')
log.info('Some messages may be too long')
log.error('Error message')
log.bye('Goodbye!')
7 changes: 5 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ For users who need to connect outside the office, we give a link like this:

http://100.100.1.2:2020/access_SeCrEtCoDe

It is possible to support other vendors' devices with help of [netmiko](https://github.com/ktbyers/netmiko), but have MikroTik-s only.
It is possible to support other vendors' devices with help of [netmiko](https://github.com/ktbyers/netmiko), but I only have MikroTik-s.

## Setup
### Option 1: EXE
Expand Down Expand Up @@ -40,7 +40,10 @@ For white and black lists, make the rules according to your needs. For example,
dst-port=80 action=dst-nat to-addresses=192.168.0.10 to-ports=80

### Only in Windows:
In console window: press «i» to print out IP addresses, «u» to print out users.
In console window:
- press «i» to print out IP addresses;
- «u» to print out users;
- «s» to reload settings so you can add new users without restart.

## Support project
- [Donate via PayPal](https://www.paypal.me/vikil)
Expand Down
5 changes: 4 additions & 1 deletion readme_ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
dst-port=80 action=dst-nat to-addresses=192.168.0.10 to-ports=80

### Только для Windows:
В консоли: нажмите «i» для вывода списка IP адресов, «u» для вывода списка пользователей
В консоли:
- нажмите «i» для вывода списка IP адресов;
- «u» для вывода списка пользователей
- «s» для перезагрузки настроек, чтобы добавлять пользователей без перезапуска.

## Помощь проекту
- Расскажите о проекте друзьям
Expand Down
Loading

0 comments on commit 911ced8

Please sign in to comment.