Skip to content

Instantly share code, notes, and snippets.

@gormih
Forked from giumas/Logging - SQLite handler
Last active January 20, 2022 08:45
Show Gist options
  • Select an option

  • Save gormih/09d18e7da67271b79b6cb3537ebfa4f3 to your computer and use it in GitHub Desktop.

Select an option

Save gormih/09d18e7da67271b79b6cb3537ebfa4f3 to your computer and use it in GitHub Desktop.

Revisions

  1. gormih revised this gist Jan 20, 2022. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions python_logging_sqlite_handler.py
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,8 @@
    from __future__ import absolute_import, division, print_function, unicode_literals

    import sqlite3
    import logging
    import time

    __version__ = "0.1.0"
    __version__ = "0.2.0"


    initial_sql = """CREATE TABLE IF NOT EXISTS log(
  2. gormih revised this gist Jan 20, 2022. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions python_logging_sqlite_handler.py
    Original file line number Diff line number Diff line change
    @@ -84,6 +84,6 @@ def emit(self, record):

    # Insert the log record
    sql = insertion_sql % record.__dict__
    conn = sqlite3.connect(self.db)
    conn.execute(sql)
    conn.commit() # not efficient, but hopefully thread-safe
    with sqlite3.connect(self.db) as conn:
    conn.execute(sql)
    conn.commit() # not efficient, but hopefully thread-safe
  3. gormih renamed this gist Jan 20, 2022. 1 changed file with 0 additions and 0 deletions.
  4. gmas revised this gist Sep 27, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Logging - SQLite handler
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,8 @@ import sqlite3
    import logging
    import time

    __version__ = "0.1.0"


    initial_sql = """CREATE TABLE IF NOT EXISTS log(
    TimeStamp TEXT,
  5. gmas created this gist Sep 27, 2015.
    87 changes: 87 additions & 0 deletions Logging - SQLite handler
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    from __future__ import absolute_import, division, print_function, unicode_literals

    import sqlite3
    import logging
    import time


    initial_sql = """CREATE TABLE IF NOT EXISTS log(
    TimeStamp TEXT,
    Source TEXT,
    LogLevel INT,
    LogLevelName TEXT,
    Message TEXT,
    Args TEXT,
    Module TEXT,
    FuncName TEXT,
    LineNo INT,
    Exception TEXT,
    Process INT,
    Thread TEXT,
    ThreadName TEXT
    )"""

    insertion_sql = """INSERT INTO log(
    TimeStamp,
    Source,
    LogLevel,
    LogLevelName,
    Message,
    Args,
    Module,
    FuncName,
    LineNo,
    Exception,
    Process,
    Thread,
    ThreadName
    )
    VALUES (
    '%(dbtime)s',
    '%(name)s',
    %(levelno)d,
    '%(levelname)s',
    '%(msg)s',
    '%(args)s',
    '%(module)s',
    '%(funcName)s',
    %(lineno)d,
    '%(exc_text)s',
    %(process)d,
    '%(thread)s',
    '%(threadName)s'
    );
    """


    class SQLiteHandler(logging.Handler):
    """
    Thread-safe logging handler for SQLite.
    """

    def __init__(self, db='app.db'):
    logging.Handler.__init__(self)
    self.db = db
    conn = sqlite3.connect(self.db)
    conn.execute(initial_sql)
    conn.commit()

    def format_time(self, record):
    """
    Create a time stamp
    """
    record.dbtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(record.created))

    def emit(self, record):
    self.format(record)
    self.format_time(record)
    if record.exc_info: # for exceptions
    record.exc_text = logging._defaultFormatter.formatException(record.exc_info)
    else:
    record.exc_text = ""

    # Insert the log record
    sql = insertion_sql % record.__dict__
    conn = sqlite3.connect(self.db)
    conn.execute(sql)
    conn.commit() # not efficient, but hopefully thread-safe