{"id":23974005,"url":"https://github.com/michdo93/python-openhab-logsaver","last_synced_at":"2026-05-13T08:36:44.073Z","repository":{"id":60009590,"uuid":"540594285","full_name":"Michdo93/python-openhab-logsaver","owner":"Michdo93","description":"Save openHAB logs into a database.","archived":false,"fork":false,"pushed_at":"2022-09-25T14:10:18.000Z","size":103,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-04-23T23:47:00.071Z","etag":null,"topics":["habapp","habapp-logs","mariadb","mariadb-database","mariadb-mysql","openhab","openhab-logs","openhab2","openhab3","python","python3"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Michdo93.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-09-23T19:46:21.000Z","updated_at":"2022-09-23T19:49:16.000Z","dependencies_parsed_at":"2022-09-25T14:52:28.236Z","dependency_job_id":null,"html_url":"https://github.com/Michdo93/python-openhab-logsaver","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Michdo93%2Fpython-openhab-logsaver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Michdo93%2Fpython-openhab-logsaver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Michdo93%2Fpython-openhab-logsaver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Michdo93%2Fpython-openhab-logsaver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Michdo93","download_url":"https://codeload.github.com/Michdo93/python-openhab-logsaver/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240511300,"owners_count":19813236,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["habapp","habapp-logs","mariadb","mariadb-database","mariadb-mysql","openhab","openhab-logs","openhab2","openhab3","python","python3"],"created_at":"2025-01-07T05:20:40.028Z","updated_at":"2026-05-13T08:36:44.045Z","avatar_url":"https://github.com/Michdo93.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Python openHAB LogSaver\nSave openhab logs into a MariaDB database. You can simply rewrite this code for other databases too. Please consider that it will only read and save the last line of a log file. If you restart openhab the log file should be empty. After each file change the last line will be reread with this program. With this each change should be transmitted into your databse table.\n\nThe table looks like:\n\n```\n+-------------+-------------+------+-----+---------+----------------+\n| Field       | Type        | Null | Key | Default | Extra          |\n+-------------+-------------+------+-----+---------+----------------+\n| id          | int(11)     | NO   | PRI | NULL    | auto_increment |\n| datetime    | datetime    | YES  | MUL | NULL    |                |\n| log_level   | varchar(10) | YES  |     | NULL    |                |\n| log_event   | varchar(30) | YES  |     | NULL    |                |\n| log_message | longtext    | YES  |     | NULL    |                |\n+-------------+-------------+------+-----+---------+----------------+\n```\n\n## Installation\n\nPlease make sure that MariaDB is installed:\n\n```\nsudo apt update\nsudo apt install mariadb-server\nsudo mysql_secure_installation\n```\n\nAfter that you have to create an admin user:\n\n```\nsudo mariadb\nGRANT ALL ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;\nFLUSH PRIVILEGES;\nexit\n```\n\nYou can now login into the database with:\n\n```\nsudo mysql -u root -p\n```\n\nIf it works you can check the systemd service with:\n\n```\nsudo systemctl status mariadb.service\n```\n\nWith following commands you can start and enable MariaDB:\n\n```\nsudo systemctl start mariadb.service\nsudo systemctl enable mariadb.service\n```\n\nI recommend creating a restricted user for the next step. In my example I use a persistence with openHAB and have created the user `openhab`. The openHAB persistence uses the database `OpenHAB` in my program for storing the openHAB logs I use the database `OpenHAB_LOGS` with this user. But it is optional which users you configure for which database and which rights these users have.\n\nThe next step is to install MariaDB for Python 3:\n\n\n```\nsudo apt-get update\nsudo apt-get install libmariadb-dev libmariadbclient-dev\n```\n\nAnd last but not least you can install this programm by using pip:\n\n```\npython3 -m pip install python-openhab-logsaver\n```\n\n## Usage\n\nYou have to create a python program to for using this logsaver. As example with\n\n```\ntouch /home/\u003cuser\u003e/logsaver.py\nchmod +x /home/\u003cuser\u003e/logsaver.py\n```\n\nLater you can run this program with:\n\n```\npython3 /home/\u003cuser\u003e/logsaver.py\n```\n\nPlease make sure that you have configured the database access in the program accordingly!\n\nTo run the program permanently after system startup, you have to create a systemd service:\n\n```\nsudo nano /etc/systemd/system/ohlogsaver.service\n```\n\nAdd the following:\n\n```\n[Unit]\nDescription=openHAB LogSaver service\nAfter=multi-user.target mariadb.service\n\n[Service]\nType=simple\nUser=\u003cuser\u003e\nGroup=\u003cuser\u003e\nRestart=always\nExecStart=/usr/bin/python3 /home/\u003cuser\u003e/logsaver.py\n\n[Install]\nWantedBy=multi-user.target\n```\n\nPlease make sure that you have to replace `\u003cuser\u003e` with the username of your current user!\n\nThen you have to start and enable it:\n\n```\nsudo systemctl daemon-reload\nsudo systemctl start ohlogsaver.service\nsudo systemctl enable ohlogsaver.service\n```\n\n### Standard configuration:\n\n```\nfrom openhab import LogSaver\n\nif __name__ == \"__main__\":\n    threads = []\n\n    db_user = \"\u003cusername\u003e\"\n    db_password = \"\u003cpassword\u003e\"\n    host = \"\u003cdatabase_ip\u003e\"\n    port = \u003cport\u003e\n    database = \"OpenHAB_LOGS\"\n    location = \"/var/log/openhab/\"\n\n    events = LogSaver(db_user, db_password, host, port, database, \"events\", location, \"events.log\")\n    openhab = LogSaver(db_user, db_password, host, port, database, \"openhab\", location, \"openhab.log\")\n\n    threads.append(events)\n    threads.append(openhab)\n\n    for th in threads:\n        th.start()\n\n    for t in threads:\n        t.join()\n\n    events.disconnect()\n    openhab.disconnect()\n```\n\n### openHAB and HABApp configuration\n\n```\nfrom openhab import LogSaver\n\nif __name__ == \"__main__\":\n    threads = []\n\n    db_user = \"\u003cusername\u003e\"\n    db_password = \"\u003cpassword\u003e\"\n    host = \"\u003cdatabase_ip\u003e\"\n    port = \u003cport\u003e\n    database = \"OpenHAB_LOGS\"\n    location = \"/var/log/openhab/\"\n    habapp_location = \"/etc/openhab/habapp/logs/\"\n\n    events = LogSaver(db_user, db_password, host, port, database, \"events\", location, \"events.log\")\n    openhab = LogSaver(db_user, db_password, host, port, database, \"openhab\", location, \"openhab.log\")\n    HABApp_events = LogSaver(db_user, db_password, host, port, database, \"HABApp_events\", habapp_location, \"HABApp_events.log\")\n    HABApp = LogSaver(db_user, db_password, host, port, database, \"HABApp\", habapp_location, \"HABApp.log\")\n\n    threads.append(events)\n    threads.append(openhab)\n    threads.append(HABApp_events)\n    threads.append(HABApp)\n\n    for th in threads:\n        th.start()\n\n    for t in threads:\n        t.join()\n\n    events.disconnect()\n    openhab.disconnect()\n```\n\n### Only read log files\n\nIf you only want to read the log files you can import the `LogReader` instead of the `LogSaver`:\n\n```\nfrom openhab import LogReader\n\nif __name__ == \"__main__\":\n    threads = []\n\n    location = \"/var/log/openhab/\"\n\n    events = LogReader(location, \"events.log\")\n    openhab = LogReader(location, \"openhab.log\")\n\n    threads.append(events)\n    threads.append(openhab)\n\n    for th in threads:\n        th.start()\n\n    for t in threads:\n        t.join()\n```\n\n## Custom configuration\n\n### Database connection\n\nAs already mentioned you have to configure your own database access:\n\n```\ndb_user = \"\u003cusername\u003e\"\ndb_password = \"\u003cpassword\u003e\"\nhost = \"\u003cdatabase_ip\u003e\"\nport = 3306\n```\n\nAs example the `\u003cusername\u003e` could be `openhab` and the `\u003cpassword\u003e` could be `anonymous`. If you run the database on the same server than your database you can use `127.0.0.1` for `\u003cdatabase_ip\u003e`. As you can see MariaDB runs under the port `3306`. If you use another port you should change that! In the example above we will create and use the database `OpenHAB_LOGS`. If you want to use another database you should rename it! Please be careful: The database will only created if it's not existing.\n\nThe variables will be used later.\n\n### Database table\n\nIn the constructor you have to use a name for your table. You can define a variable like this:\n\n```\ntable = \"logs\"\n```\n\nThe table `logs` will be created inside your configured database if it is not exsisting! If you want to use another table you should rename `logs`.\n\nNormally openHAB will use the two log files `events.log` and `openhab.log`. So you could use two Threads which will read each of this files and could also create/use two tables. Or if you use HABApp you there are maybe other log files you want to save in your database.\n\n### Log Path\n\nThe log path for openHAB 2 and openHAB 3 differs. The path `/var/log/openhab/` is used by openHAB 3 for saving the logs. If you use as example openHAB 2 you want to change the path to `/var/log/openhab2/`. Please make sure that you use a `/` at the end of the path!\n\n```\nlocation = \"/var/log/openhab\"\n```\n\nor\n\n```\nlocation = \"/var/log/openhab2\"\n```\n\nThis path may vary if you installed openHAB manually or via Docker. If you are using Docker, you must specify the path to the volume. And of course you can use another path if you are using HABApp.\n\n### How to read a log file\n\nIf you want to read a log file you have to run as example:\n\n```\nevents = OpenHABLogReader(db_user, db_password, host, port, database, \"events\", location, \"events.log\")\nopenhab = OpenHABLogReader(db_user, db_password, host, port, database, \"openhab\", location, \"openhab.log\")\n```\n\nThis are two Threads which could be started. As you can see, some parameters are used twice for the constructors, so the use of variables made sense. If e.g. HABApp is added now, you might want to enter the path for the log data there via another variable or directly in the constructor.\n\nThis will read the `/var/log/openhab/events.log` and write its content into the database table `events`. And it will read `/var/log/openhab/openhab.log` and write its content into the database table `openhab`. Of course it is possible that you save it into the same database table.\n\n### Add HABApp Logs\n\nIf you want to add as example the HABApp logs to this program for also saving them into the database you have to make sure that the HABApp logs are saved in the same location as the openhab logs or use another object. Please edit the `config.yml` (as example in `/etc/openhab/habapp/`):\n\n```\n...\ndirectories:\n  logging: /var/log/openhab  # Folder where the logs will be written to\n...\n```\n\n```\nHABApp_events = OpenHABLogReader(db_user, db_password, host, port, database, \"HABApp_events\", \"/etc/openhab/habapp/logs\", \"HABApp_events.log\")\nHABApp = OpenHABLogReader(db_user, db_password, host, port, database, \"HABApp\", \"/etc/openhab/habapp/logs\", \"HABApp.log\")\n```\n\nThen you have to make sure that HABApp uses the same formatting for saving the logs than openhab. Please edit the `logging.yml` (as example in `/etc/openhab/habapp/`):\n\n```\n  HABApp_default:\n    class: HABApp.core.lib.handler.MidnightRotatingFileHandler\n    filename: '/var/log/openhab/HABApp.log'\n    maxBytes: 1_048_576\n    backupCount: 3\n\n    formatter: HABApp_format\n    level: DEBUG\n\n  EventFile:\n    class: HABApp.core.lib.handler.MidnightRotatingFileHandler\n    filename: 'HABApp_events.log'\n    maxBytes: 1_048_576\n    backupCount: 3\n\n    formatter: HABApp_format\n    level: DEBUG\n\n  BufferEventFile:\n    class: logging.handlers.MemoryHandler\n    capacity: 10\n    formatter: HABApp_format\n    target: EventFile\n    level: DEBUG\n\n\nloggers:\n  HABApp:\n    level: INFO\n    handlers:\n      - HABApp_default\n    propagate: False\n\n  HABApp.EventBus:\n    level: INFO\n    handlers:\n      - BufferEventFile\n    propagate: False\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichdo93%2Fpython-openhab-logsaver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmichdo93%2Fpython-openhab-logsaver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichdo93%2Fpython-openhab-logsaver/lists"}