# TheLogger
Easy logging, timing and email notifications of code execution.
### Installation
```
$ pip install thelogger
```
### Logging
my_script.py:
```python class:"lineNo"
from thelogger import lg
# log messages
lg.info('Hello World')
lg.warning('warning message')
lg.error('error message')
# start logging messages to a file
lg.reset(file = './demo_log.txt')
# shorthand convenience methods for logging messages
lg.i('this is an info message')
lg.w('this is a warning message')
# remove log handlers
lg.close()
lg.i('nothing is logged because there are no log handlers')
# add default log handlers
lg.reset()
```
Output to Console:
```
[I 2021-11-13 08:50:36] Hello World
[W 2021-11-13 08:50:36] warning message
[E 2021-11-13 08:50:36] error message
[I 2021-11-13 08:50:36] this is an info message
[W 2021-11-13 08:50:36] this is a warning message
```
Output to demo_log.txt:
```
[I 2021-11-13 08:50:36 my_script:12] this is an info message
[W 2021-11-13 08:50:36 my_script:13] this is a warning message
```
### Email Notifications
```python class:"lineNo"
from thelogger import lg, notify
# decorate your func with @notify and pass in your email address
@notify(email = 'my_email@gmail.com')
def concat_str(arg1, arg2=''):
return f'{arg1} {arg2}'
# when concat_str is finished executing you will receive an email with the details
my_str = concat_str('hello', 'world')
# pass a logger object to log the function execution details
@notify(email = 'my_email@gmail.com', logger = lg)
def concat_str(arg1, arg2=''):
return f'{arg1} {arg2}'
my_str = concat_str('hello', 'world')
# include a remote host address if your organization has gmail blocked
@notify(email = 'my_email@gmail.com', logger = lg, host = 'mail.abc.com')
def concat_str(arg1, arg2=''):
return f'{arg1} {arg2}'
my_str = concat_str('hello', 'world')
# quick test to see if you're able to receive emails
notify('my_email@gmail.com', test = True)
# set the default arguments for the notify decorator
notify = notify(email = 'my_email@gmail.com', logger = lg, setdefault = True)
# now you can decorate functions w/o passing the args to @notify each time
@notify
def concat_str(arg1, arg2=''):
return f'{arg1} {arg2}'
my_str = concat_str('hello', 'world')
```
Example Email:
![alt text](https://github.com/tom1919/TheLogger/blob/main/example_email.PNG)
### "Free" Logging of Scripts
Leverage TheLogger to easily log a program's execution details without typing out any log messages. See below example script and log file.
my_program.py:
```python class:"lineNo"
import numpy as np
import pandas as pd
from thelogger import notify, lg
lg = lg.reset(file = 'my_program.log')
notify = notify(setdefault=True, logger=lg)
@notify
def get_data(arg1):
data = pd.DataFrame(np.random.randint(0,100,size=(10**7, 50)))
return data
@notify
def wrangle_data(data):
data2 = data.iloc[0:5,0:5] + 1
return data2
@notify
def distribute_data(data2):
print(data2)
@notify
def execute_program(program_name):
data = get_data('my_arg')
data2 = wrangle_data(data)
distribute_data(data2)
return 'success'
execute_program('my_program')
```
Output to my_program.log:
```
[I 2022-04-19 20:43:56 my_program:36] Starting: execute_program('my_program')...
[I 2022-04-19 20:43:56 my_program:31] Starting: data = get_data('my_arg')...
[I 2022-04-19 20:43:59 my_program:31] Finished: data = get_data('my_arg')...
[I 2022-04-19 20:44:00 my_program:31] Inputs and Output of get_data:
| Variable | Type | Length | String |
|------------|-------------------------------|----------|-------------------------------------------------------------|
| arg1 | 'str' | 6 | my_arg |
| output | 'pandas.core.frame.DataFrame' | 10000000 | cols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ... |
Elapsed Time: 0:00:03.479504
[I 2022-04-19 20:44:00 my_program:32] Starting: data2 = wrangle_data(data)...
[I 2022-04-19 20:44:00 my_program:32] Finished: data2 = wrangle_data(data)...
[I 2022-04-19 20:44:00 my_program:32] Inputs and Output of wrangle_data:
| Variable | Type | Length | String |
|------------|-------------------------------|----------|-------------------------------------------------------------|
| data | 'pandas.core.frame.DataFrame' | 10000000 | cols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ... |
| output | 'pandas.core.frame.DataFrame' | 5 | cols: 0, 1, 2, 3, 4... |
Elapsed Time: 0:00:00.000718
[I 2022-04-19 20:44:00 my_program:33] Starting: distribute_data(data2)...
[I 2022-04-19 20:44:00 my_program:33] Finished: distribute_data(data2)...
[I 2022-04-19 20:44:00 my_program:33] Inputs and Output of distribute_data:
| Variable | Type | Length | String |
|------------|-------------------------------|----------|------------------------|
| data2 | 'pandas.core.frame.DataFrame' | 5 | cols: 0, 1, 2, 3, 4... |
| output | 'NoneType' | nan | None |
Elapsed Time: 0:00:00.004332
[I 2022-04-19 20:44:00 my_program:36] Finished: execute_program('my_program')...
[I 2022-04-19 20:44:00 my_program:36] Inputs and Output of execute_program:
| Variable | Type | Length | String |
|--------------|--------|----------|------------|
| program_name | 'str' | 10 | my_program |
| output | 'str' | 7 | success |
Elapsed Time: 0:00:03.69315
```
Raw data
{
"_id": null,
"home_page": null,
"name": "thelogger",
"maintainer": null,
"docs_url": null,
"requires_python": ">3.8",
"maintainer_email": null,
"keywords": "log, logging, logger, email, timimg, notification",
"author": null,
"author_email": null,
"download_url": null,
"platform": null,
"description": "# TheLogger\n\nEasy logging, timing and email notifications of code execution.\n\n### Installation\n\n```\n$ pip install thelogger\n```\n\n### Logging\n\nmy_script.py:\n```python class:\"lineNo\"\nfrom thelogger import lg\n\n# log messages\nlg.info('Hello World')\nlg.warning('warning message')\nlg.error('error message')\n\n# start logging messages to a file\nlg.reset(file = './demo_log.txt')\n\n# shorthand convenience methods for logging messages\nlg.i('this is an info message')\nlg.w('this is a warning message')\n\n# remove log handlers\nlg.close()\nlg.i('nothing is logged because there are no log handlers')\n\n# add default log handlers\nlg.reset()\n```\n\nOutput to Console:\n```\n[I 2021-11-13 08:50:36] Hello World\n[W 2021-11-13 08:50:36] warning message\n[E 2021-11-13 08:50:36] error message\n[I 2021-11-13 08:50:36] this is an info message\n[W 2021-11-13 08:50:36] this is a warning message\n```\n\nOutput to demo_log.txt:\n```\n[I 2021-11-13 08:50:36 my_script:12] this is an info message\n[W 2021-11-13 08:50:36 my_script:13] this is a warning message\n```\n\n### Email Notifications\n```python class:\"lineNo\"\nfrom thelogger import lg, notify\n\n# decorate your func with @notify and pass in your email address \n@notify(email = 'my_email@gmail.com')\ndef concat_str(arg1, arg2=''):\n return f'{arg1} {arg2}'\n# when concat_str is finished executing you will receive an email with the details\nmy_str = concat_str('hello', 'world')\n\n# pass a logger object to log the function execution details\n@notify(email = 'my_email@gmail.com', logger = lg)\ndef concat_str(arg1, arg2=''):\n return f'{arg1} {arg2}'\nmy_str = concat_str('hello', 'world')\n\n# include a remote host address if your organization has gmail blocked\n@notify(email = 'my_email@gmail.com', logger = lg, host = 'mail.abc.com')\ndef concat_str(arg1, arg2=''):\n return f'{arg1} {arg2}'\nmy_str = concat_str('hello', 'world')\n\n# quick test to see if you're able to receive emails\nnotify('my_email@gmail.com', test = True)\n\n# set the default arguments for the notify decorator\nnotify = notify(email = 'my_email@gmail.com', logger = lg, setdefault = True)\n# now you can decorate functions w/o passing the args to @notify each time\n@notify\ndef concat_str(arg1, arg2=''):\n return f'{arg1} {arg2}'\nmy_str = concat_str('hello', 'world')\n```\n\nExample Email:\n\n![alt text](https://github.com/tom1919/TheLogger/blob/main/example_email.PNG)\n\n### \"Free\" Logging of Scripts\nLeverage TheLogger to easily log a program's execution details without typing out any log messages. See below example script and log file.\n\nmy_program.py:\n```python class:\"lineNo\"\nimport numpy as np\nimport pandas as pd\nfrom thelogger import notify, lg\n\nlg = lg.reset(file = 'my_program.log')\nnotify = notify(setdefault=True, logger=lg)\n\n@notify\ndef get_data(arg1):\n data = pd.DataFrame(np.random.randint(0,100,size=(10**7, 50)))\n return data\n\n@notify\ndef wrangle_data(data):\n data2 = data.iloc[0:5,0:5] + 1\n return data2\n\n@notify\ndef distribute_data(data2):\n print(data2)\n\n@notify\ndef execute_program(program_name):\n data = get_data('my_arg')\n data2 = wrangle_data(data)\n distribute_data(data2)\n return 'success'\n\nexecute_program('my_program')\n```\n\nOutput to my_program.log:\n```\n[I 2022-04-19 20:43:56 my_program:36] Starting: execute_program('my_program')...\n[I 2022-04-19 20:43:56 my_program:31] Starting: data = get_data('my_arg')...\n[I 2022-04-19 20:43:59 my_program:31] Finished: data = get_data('my_arg')...\n[I 2022-04-19 20:44:00 my_program:31] Inputs and Output of get_data:\n| Variable | Type | Length | String |\n|------------|-------------------------------|----------|-------------------------------------------------------------|\n| arg1 | 'str' | 6 | my_arg |\n| output | 'pandas.core.frame.DataFrame' | 10000000 | cols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ... |\nElapsed Time: 0:00:03.479504\n[I 2022-04-19 20:44:00 my_program:32] Starting: data2 = wrangle_data(data)...\n[I 2022-04-19 20:44:00 my_program:32] Finished: data2 = wrangle_data(data)...\n[I 2022-04-19 20:44:00 my_program:32] Inputs and Output of wrangle_data:\n| Variable | Type | Length | String |\n|------------|-------------------------------|----------|-------------------------------------------------------------|\n| data | 'pandas.core.frame.DataFrame' | 10000000 | cols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ... |\n| output | 'pandas.core.frame.DataFrame' | 5 | cols: 0, 1, 2, 3, 4... |\nElapsed Time: 0:00:00.000718\n[I 2022-04-19 20:44:00 my_program:33] Starting: distribute_data(data2)...\n[I 2022-04-19 20:44:00 my_program:33] Finished: distribute_data(data2)...\n[I 2022-04-19 20:44:00 my_program:33] Inputs and Output of distribute_data:\n| Variable | Type | Length | String |\n|------------|-------------------------------|----------|------------------------|\n| data2 | 'pandas.core.frame.DataFrame' | 5 | cols: 0, 1, 2, 3, 4... |\n| output | 'NoneType' | nan | None |\nElapsed Time: 0:00:00.004332\n[I 2022-04-19 20:44:00 my_program:36] Finished: execute_program('my_program')...\n[I 2022-04-19 20:44:00 my_program:36] Inputs and Output of execute_program:\n| Variable | Type | Length | String |\n|--------------|--------|----------|------------|\n| program_name | 'str' | 10 | my_program |\n| output | 'str' | 7 | success |\nElapsed Time: 0:00:03.69315\n```\n\n\n",
"bugtrack_url": null,
"license": "Apache 2.0",
"summary": "Easy logging, timing and email notifications of code execution.",
"version": "0.4.3",
"project_urls": null,
"split_keywords": [
"log",
" logging",
" logger",
" email",
" timimg",
" notification"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0675e6da1a280a0933416e46e115ae0fde24b8a683519e6a9e908f256114ddf0",
"md5": "a23915892ece84acf78ed898157f6738",
"sha256": "30e3f1389ea741a7cdcbe2ada7c42638a9a63dcb9870ac4ffbaed7839e92eb6d"
},
"downloads": -1,
"filename": "thelogger-0.4.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "a23915892ece84acf78ed898157f6738",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">3.8",
"size": 16062,
"upload_time": "2024-08-09T04:10:38",
"upload_time_iso_8601": "2024-08-09T04:10:38.741130Z",
"url": "https://files.pythonhosted.org/packages/06/75/e6da1a280a0933416e46e115ae0fde24b8a683519e6a9e908f256114ddf0/thelogger-0.4.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-09 04:10:38",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "thelogger"
}