Skip to content

Commit 45f4af0

Browse files
committed
adding logging notes, files
1 parent 916450b commit 45f4af0

File tree

6 files changed

+97
-0
lines changed

6 files changed

+97
-0
lines changed

logging.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
07/28/2021 09:37:19 - root - WARNING - this is a warning message
2+
07/28/2021 09:37:19 - root - ERROR - this is an error message
3+
07/28/2021 09:37:19 - root - CRITICAL - this is a critical message
4+
07/28/2021 09:37:55 - root - DEBUG - This is a debug message
5+
07/28/2021 09:37:55 - root - INFO - This is an info message
6+
07/28/2021 09:37:55 - root - WARNING - this is a warning message
7+
07/28/2021 09:37:55 - root - ERROR - this is an error message
8+
07/28/2021 09:37:55 - root - CRITICAL - this is a critical message

logging/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Logging
2+
3+
### Can use a ConfigFile
4+
5+
logging.conf or logging.ini
6+
7+
```python
8+
import logging.config
9+
10+
logging.config.fileConfig('logging.config')
11+
12+
13+
```
14+
15+
```python
16+
import logging.config
17+
18+
logging.config.dictConfig('logging.config')
19+
20+
```
21+
22+
### Capturing Stacktraces

logging/exceptions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import logging
2+
import traceback
3+
4+
try:
5+
a = [1,2,3]
6+
val = a[4]
7+
except Exception as e:
8+
#logging.error(e, exc_info=True)
9+
logging.error(f"The Error is: {traceback.format_exc()}")

logging/helper.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import logging
2+
3+
logging.basicConfig(level=logging.DEBUG, filename='logging.txt',
4+
filemode='a',
5+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
6+
datefmt='%m/%d/%Y %H:%M:%S'
7+
)
8+
9+
10+
logging.debug("This is a debug message")
11+
logging.info("This is an info message")
12+
# these logged by default, can set a level higher like debug if desired.
13+
logging.warning("this is a warning message")
14+
logging.error("this is an error message")
15+
logging.critical("this is a critical message")

logging/main.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import logging
2+
3+
# grabs the name of the file for the log message
4+
# good practice to add to all modules
5+
logger = logging.getLogger(__name__)
6+
7+
# create handler
8+
stream_handler = logging.StreamHandler()
9+
file_handler = logging.FileHandler('file.log')
10+
11+
# set level and format
12+
stream_handler.setLevel(logging.WARNING)
13+
file_handler.setLevel(logging.ERROR)
14+
15+
formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
16+
stream_handler.setFormatter(formatter)
17+
file_handler.setFormatter(formatter)
18+
19+
logger.addHandler(stream_handler)
20+
logger.addHandler(file_handler)
21+
22+
logger.warning("This is a warning")
23+
logger.error("This is an error")

logging/rotatinghandlers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import logging
2+
# https://docs.python.org/3/library/logging.handlers.html
3+
from logging.handlers import RotatingFileHandler, TimedRotatingFileHandler
4+
import time
5+
6+
logger = logging.getLogger(__name__)
7+
logger.setLevel(logging.INFO)
8+
9+
# roll over after 2KB, and keep backup logs...
10+
handler_size = RotatingFileHandler('app.log', mode='a', maxBytes=2000, backupCount=5)
11+
12+
# when: s, m, h, d, midnight, w0
13+
handler_time = TimedRotatingFileHandler('timed_log.log', when='s', interval=5, backupCount=5)
14+
15+
16+
logger.addHandler(handler_time)
17+
18+
for _ in range(20):
19+
time.sleep(1)
20+
logger.info("This is some text, watch it pile up over time.")

0 commit comments

Comments
 (0)