{"id":16526467,"url":"https://github.com/jmmv/db_logger","last_synced_at":"2025-10-28T09:30:19.781Z","repository":{"id":42124750,"uuid":"478985673","full_name":"jmmv/db_logger","owner":"jmmv","description":"A database-backed logger for use with the log crate","archived":false,"fork":false,"pushed_at":"2023-09-30T20:48:50.000Z","size":109,"stargazers_count":7,"open_issues_count":2,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-01T13:35:17.580Z","etag":null,"topics":["database","logging","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jmmv.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"jmmv"}},"created_at":"2022-04-07T12:57:13.000Z","updated_at":"2023-10-03T08:50:19.000Z","dependencies_parsed_at":"2022-08-12T07:20:10.080Z","dependency_job_id":null,"html_url":"https://github.com/jmmv/db_logger","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmmv%2Fdb_logger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmmv%2Fdb_logger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmmv%2Fdb_logger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmmv%2Fdb_logger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jmmv","download_url":"https://codeload.github.com/jmmv/db_logger/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238623749,"owners_count":19503094,"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":["database","logging","rust"],"created_at":"2024-10-11T17:28:16.662Z","updated_at":"2025-10-28T09:30:19.156Z","avatar_url":"https://github.com/jmmv.png","language":"Rust","funding_links":["https://github.com/sponsors/jmmv"],"categories":[],"sub_categories":[],"readme":"# A database-backed logger for use with the log crate\n\n[![crates.io](https://img.shields.io/crates/v/db_logger.svg)](https://crates.io/crates/db_logger/)\n[![docs.rs](https://docs.rs/db_logger/badge.svg)](https://docs.rs/db_logger/)\n\ndb\\_logger is a Rust crate providing an implementation of the\n[log crate](https://crates.io/crates/log)'s logging facade to write structured\nlog entries to a database.  Just add a few lines of code to the beginning of\nyour program and all logging will be saved for later analysis, which is\nespecially suited to (distributed) services.\n\ndb\\_logger currently supports PostgreSQL and SQLite and is backed by the\n[sqlx crate](https://crates.io/crates/sqlx).\n\n**The latest version of db\\_logger is 0.1.0 and was released on 2022-04-12.**\n\n# Usage\n\nTo use db\\_logger, you need to add a dependency to your project with the right\nset of features, create a database with the expected schema, and then initialize\nthe logger during program initialization.\n\nAs a logging facade implementation, db\\_logger should only be depended upon from\nbinary crates (never from libraries).\n\n## Usage with PostgreSQL\n\n1.  Add the following to your list of dependencies in `Cargo.toml`:\n\n    ```toml\n    [dependencies.db_logger]\n    version = \"0.1\"\n    default-features = false\n    features = [\"postgres\"]\n    ```\n\n1.  Create a PostgreSQL database and initialize it with the\n    [`schemas/postgres.sql`](schemas/postgres.sql) schema.\n\n1.  Initialize the logger in your code with one of:\n\n    *   Explicit configuration:\n\n        ```rust\n        use db_logger::postgres;\n        let conn = postgres::connect_lazy(postgres::ConnectionOptions {\n            host: \"some host\".to_owned(),\n            port: 5432,\n            database: \"some database\".to_owned(),\n            username: \"some username\".to_owned(),\n            password: \"some password\".to_owned(),\n            ..Default::default()\n        });\n        let _handle = db_logger::init(conn).await;\n        ```\n\n    *   Environment-based configuration:\n\n        ```rust\n        use db_logger::postgres;\n        let conn = postgres::connect_lazy(\n            postgres::ConnectionOptions::from_env(\"LOGGER\").unwrap());\n        let _handle = db_logger::init(conn).await;\n        ```\n\n        This will cause your program to recognize variables of the form\n        `LOGGER_HOST`, `LOGGER_PORT`, `LOGGER_DATABASE`, `LOGGER_USERNAME` and\n        `LOGGER_PASSWORD` to configure the PostgreSQL connection.\n\n1.  Make sure to keep `_handle` alive for the duration of the program in an\n    async context, because the handle keeps the background logging task alive.\n\n## Usage with SQLite\n\n1.  Add the following to your list of dependencies in `Cargo.toml`:\n\n    ```toml\n    [dependencies.db_logger]\n    version = \"0.1\"\n    default-features = false\n    features = [\"sqlite\"]\n    ```\n\n1.  Create an SQLite database and initialize it with the\n    [`schemas/sqlite.sql`](schemas/sqlite.sql) schema.\n\n1.  Initialize with:\n\n    ```rust\n    use db_logger::sqlite;\n    let conn = sqlite::connect(sqlite::ConnectionOptions {\n        uri: \"file:/path/to/database?mode=rw\",\n        ..Default::default()\n    }).await.unwrap();\n    let _handle = db_logger::init(conn).await;\n    ```\n\n1.  Make sure to keep `_handle` alive for the duration of the program in an\n    async context, because the handle keeps the background logging task alive.\n\n## Environment configuration\n\ndb\\_logger recognizes the `RUST_LOG` environment variable to configure the\nmaximum level of the log messages to record, the same way as the\n[env\\_logger crate](https://crates.io/crates/env_logger) does.\n\n## Schema initialization\n\nAs indicated above, you should create the database and its schema by hand\nbefore establishing a connection using the reference files provided in the\n[`schemas`](schemas) directory.  This is a one-time operation.\n\nYou also have the option of invoking the `Connection::create_schema()` method\nto initialize the database schema.  You probably don't want to do this in\nproduction but this is useful if you are using ephemeral SQLite databases.\n\n# Limitations\n\nThe code in this crate was extracted from the\n[EndBASIC cloud service](https://www.endbasic.dev/service.html) and then cleaned\nfor separate publication.  This code was written as a fun experiment as part of\nthat service and it has not received a lot of real-world stress testing.  As a\nresult, expect this to have a bunch of limitations, including:\n\n*   Because db\\_logger runs in-process, the logger must filter out any log\n    messages that it can generate while writing to the database (or else it\n    would enter an infinite logging loop).  This includes log messages for its\n    own dependencies, such as those emitted by sqlx or tokio, which means that\n    if you are using this crate, you won't get their logs saved into the\n    database.\n\n*   Performance is not great.  While the logger tries to avoid blocking caller\n    code by buffering log messages, if you log a lot, your application will\n    slow down.  I'm positive some profiling could make this much better without\n    a ton of effort, but I just haven't spent the time doing it.\n\n*   No tracing support.  This crate may be a dead end.  Saving raw logs to a\n    database is interesting but what would be more interesting is saving the\n    traces generated when integrating with the [tracing\n    crate](https://crates.io/crates/tracing) (so as to trace individual\n    requests as they flow through an async server, which was the original\n    desire).\n\n*   stderr pollution.  Right now, any errors encountered while persisting logs\n    to the database and any log messages that are filtered out are dumped to\n    stderr in an ad-hoc format.  This behavior should be configurable but isn't.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmmv%2Fdb_logger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjmmv%2Fdb_logger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmmv%2Fdb_logger/lists"}