@@ -1,59 +1,44 | |||||
1 | """ |
|
1 | ''' | |
2 | SCHAINPY - LOG |
|
2 | SCHAINPY - LOG | |
3 | Simple helper for log standarization |
|
3 | Simple helper for log standarization | |
4 | Usage: |
|
4 | Usage: | |
5 | from schainpy.utils import log |
|
5 | from schainpy.utils import log | |
6 | log.error('A kitten died beacuse of you') |
|
6 | log.error('A kitten died beacuse of you') | |
7 | log.warning('You are doing it wrong but what the heck, I'll allow it) |
|
7 | log.warning('You are doing it wrong but what the heck, I'll allow it) | |
8 | log.succes('YOU ROCK!') |
|
8 | log.succes('YOU ROCK!') | |
9 | To create your own logger inside your class do it like this: |
|
9 | To create your own logger inside your class do it like this: | |
10 | from schainpy.utils import log |
|
10 | from schainpy.utils import log | |
11 | awesomeLogger = log.makelogger("never gonna", bg="red", fg="white") |
|
11 | awesomeLogger = log.makelogger("never gonna", bg="red", fg="white") | |
12 | awesomeLogger('give you up') |
|
12 | awesomeLogger('give you up') | |
13 | which will look like this: |
|
13 | which will look like this: | |
14 | [NEVER GONNA] - give you up |
|
14 | [NEVER GONNA] - give you up | |
15 | with color red as background and white as foreground. |
|
15 | with color red as background and white as foreground. | |
16 | """ |
|
16 | ''' | |
17 | import os |
|
17 | ||
18 | import sys |
|
|||
19 | import click |
|
18 | import click | |
20 |
|
19 | |||
21 | def warning(message): |
|
20 | def warning(message, tag='Warning'): | |
22 |
click.echo(click.style('[ |
|
21 | click.echo(click.style('[{}] {}'.format(tag, message), fg='yellow')) | |
|
22 | pass | |||
|
23 | ||||
23 |
|
24 | |||
|
25 | def error(message, tag='Error'): | |||
|
26 | click.echo(click.style('[{}] {}'.format(tag, message), fg='red')) | |||
|
27 | pass | |||
24 |
|
28 | |||
25 | def error(message): |
|
|||
26 | click.echo(click.style('[ERROR] - ' + message, fg='red', bg='black')) |
|
|||
27 |
|
29 | |||
|
30 | def success(message, tag='Info'): | |||
|
31 | click.echo(click.style('[{}] {}'.format(tag, message), fg='green')) | |||
|
32 | pass | |||
28 |
|
33 | |||
29 | def success(message): |
|
|||
30 | click.echo(click.style(message, fg='green')) |
|
|||
31 |
|
34 | |||
|
35 | def log(message, tag='Info'): | |||
|
36 | click.echo('[{}] {}'.format(tag, message)) | |||
|
37 | pass | |||
32 |
|
38 | |||
33 | def log(message, topic='LOG'): |
|
|||
34 | click.echo('[{}] - {}'.format(topic, message)) |
|
|||
35 |
|
39 | |||
36 |
def makelogger(t |
|
40 | def makelogger(tag, bg='reset', fg='reset'): | |
37 | def func(message): |
|
41 | def func(message): | |
38 |
click.echo(click.style('[{}] |
|
42 | click.echo(click.style('[{}] {}'.format(tag.upper(), message), | |
39 | bg=bg, fg=fg)) |
|
43 | bg=bg, fg=fg)) | |
40 | return func |
|
44 | return func | |
41 |
|
||||
42 | class LoggerForFile(): |
|
|||
43 | def __init__(self, filename): |
|
|||
44 | self.old_stdout=sys.stdout |
|
|||
45 | cwd = os.getcwd() |
|
|||
46 | self.log_file = open(os.path.join(cwd, filename), 'w+') |
|
|||
47 | def write(self, text): |
|
|||
48 | text = text.rstrip() |
|
|||
49 | if not text: |
|
|||
50 | return |
|
|||
51 | self.log_file.write(text + '\n') |
|
|||
52 | self.old_stdout.write(text + '\n') |
|
|||
53 | def flush(self): |
|
|||
54 | self.old_stdout.flush() |
|
|||
55 |
|
||||
56 | def logToFile(filename='log.log'): |
|
|||
57 | logger = LoggerForFile(filename) |
|
|||
58 | sys.stdout = logger |
|
|||
59 |
|
General Comments 0
You need to be logged in to leave comments.
Login now