Skip to content

Add FileHandler class #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f49b84d
Add FileHandler class
tekktrik Oct 24, 2021
1e061cf
Ran pre-commit
tekktrik Oct 26, 2021
135ed87
Fix lint
tekktrik Oct 26, 2021
fe1e075
Fix lint
tekktrik Oct 26, 2021
a8e4e64
Finalize fixing CRLF to LF
tekktrik Oct 26, 2021
aa87bd6
Changed functionality to better represent CPython's functionality
tekktrik Oct 26, 2021
42fe85a
Updated per results of pre-commit
tekktrik Oct 26, 2021
030bf6b
Updated docstring for FileHandler
tekktrik Oct 26, 2021
891a242
Create example for FileHandler using an SD card
tekktrik Oct 28, 2021
db969d9
Update example to avoid directly initializing SD card
tekktrik Oct 28, 2021
57973bc
Remove unused imports
tekktrik Oct 28, 2021
d82a56a
Remove board import
tekktrik Oct 28, 2021
7735275
Update logging_filehandler.py
tekktrik Oct 28, 2021
5782c51
Changed double quotes to singe quotes
tekktrik Oct 28, 2021
c0f29d7
Added format method for adding newlines
tekktrik Oct 28, 2021
0c8a6a3
Updated example to initialize SD card
tekktrik Oct 28, 2021
0b72f51
Updated formatting from black, using Python 3 style super()
tekktrik Oct 28, 2021
49b1224
Move adafruit_logging.py to adafruit_logging/__init__.py
tekktrik Oct 28, 2021
ecc450d
Move FileHandler to extensions.py
tekktrik Oct 28, 2021
4b2af64
Updated FileHandler example to match new structure
tekktrik Oct 28, 2021
2c3bafd
Updated formatting per black
tekktrik Oct 28, 2021
7e9924b
Add license and module docstring
tekktrik Oct 28, 2021
9fd0bc8
Merge branch 'tekktrik-feature/add-file-handler'
tekktrik Oct 31, 2021
41bd708
Merge branch 'main' of https://github.com/adafruit/Adafruit_CircuitPy…
tekktrik Nov 1, 2021
e583c86
Merge branch 'adafruit-main' into feature/add-file-handler
tekktrik Nov 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
46 changes: 46 additions & 0 deletions adafruit_logging/extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# SPDX-FileCopyrightText: 2021 Alec Delaney for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
`extensions`
====================================================

CircuitPython logging extension for logging to files

* Author(s): Alec Delaney
"""

from . import LoggingHandler


class FileHandler(LoggingHandler):
"""File handler for working with log files off of the microcontroller (like
an SD card)

:param filepath: The filepath to the log file
:param mode: Whether to write ('w') or append ('a'); default is to append
"""

def __init__(self, filepath: str, mode: str = "a"):
self.logfile = open(filepath, mode, encoding="utf-8")

def close(self):
"""Closes the file"""
self.logfile.close()

def format(self, level: int, msg: str):
"""Generate a string to log

:param level: The level of the message
:param msg: The message to format
"""
return super().format(level, msg) + "\r\n"

def emit(self, level: int, msg: str):
"""Generate the message and write it to the UART.

:param level: The level of the message
:param msg: The message to log
"""
self.logfile.write(self.format(level, msg))
30 changes: 30 additions & 0 deletions examples/logging_filehandler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# SPDX-FileCopyrightText: 2021 Alec Delaney
# SPDX-License-Identifier: MIT

import board
import busio
from digitalio import DigitalInOut
import storage
import adafruit_sdcard
import adafruit_logging as logging
from adafruit_logging.extensions import FileHandler

# Get chip select pin depending on the board, this one is for the Feather M4 Express
sd_cs = board.D10

# Set up an SD card to write to
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs = DigitalInOut(sd_cs)
sdcard = adafruit_sdcard.SDCard(spi, cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")

# Initialize log functionality
log_filepath = "/sd/testlog.log"
logger = logging.getLogger("testlog")
file_handler = FileHandler(log_filepath)
logger.addHandler(file_handler)
logger.setLevel(logging.INFO)

logger.info("Logger initialized!")
logger.debug("You can even add debug statements to the log!")