Skip to content

Instantly share code, notes, and snippets.

@jacobmartinez3d
Created June 29, 2022 16:18
Show Gist options
  • Save jacobmartinez3d/e021fbec98f9bf5abaf8afc9a23e99a7 to your computer and use it in GitHub Desktop.
Save jacobmartinez3d/e021fbec98f9bf5abaf8afc9a23e99a7 to your computer and use it in GitHub Desktop.
import getpass
import logging
import os
import socketio
class SocketIOHandler(logging.Handler):
"""A logging handler that emits records with SocketIO."""
_client = socketio.Client(logger=True, engineio_logger=True)
def __init__(self, *args, **kwargs):
super(SocketIOHandler, self).__init__()
self._client.connect("http://{host}:{port}".format(**kwargs))
def emit(self, record):
data = {
"log_record_data": record.__dict__,
"username": getpass.getuser()
}
self._client.emit("logging_event", data, callback=self._client.disconnect)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
socket_handler = SocketIOHandler(host="localhost", port=5000)
socket_handler.setLevel(logging.DEBUG)
logger.addHandler(socket_handler)
logger.critical("Critical Error! The application has encountered a critcal error and cannot continue.")
logger.warning("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.")
logger.warning("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vulputate elementum tellus. Aenean finibus velit id tortor facilisis feugiat. Nam libero libero, ullamcorper at porta interdum, sagittis sit amet elit. Maecenas quam tortor, maximus sed leo sit amet, faucibus bibendum diam. Duis ante lacus, vestibulum scelerisque nulla in, posuere auctor. ")
logger.debug(os.environ.copy())
logger.info("some info...")
logger.error("errorz")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment