Giter Club home page Giter Club logo

kv-log-macro's Introduction

kv-log-macro

crates.io version build status downloads docs.rs docs

Log macro for log's kv-unstable backend.

Examples

use kv_log_macro::info;

fn main() {
    femme::start(log::LevelFilter::Info).unwrap();
    info!("hello");
    info!("hello",);
    info!("hello {}", "cats");
    info!("hello {}", "cats",);
    info!("hello {}", "cats", {
        cat_1: "chashu",
        cat_2: "nori",
    });
}

Installation

$ cargo add kv-log-macro

Safety

This crate uses #![deny(unsafe_code)] to ensure everything is implemented in 100% Safe Rust.

Contributing

Want to join us? Check out our "Contributing" guide and take a look at some of these issues:

References

None.

License

MIT OR Apache-2.0

kv-log-macro's People

Contributors

kodraus avatar quininer avatar yoshuawuyts avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

kv-log-macro's Issues

allow log macros to reference a container of kv pairs for logging

Feature Request

Summary

I have structs which have fields I'd like to log. So I create a log_context member which holds the kv pairs.
These rarely change, and then only by appending values. The current syntax becomes unwieldy if I use
the field names and values and later refactor the struct and add additional fields I'd like to log.

Motivation

I'm writing server code and would like to efficiently log object information in addition to the normal point in code logging.

Guide-level explanation

Assume a struct has a log_context field consisting of a container of kv pairs and is referenced by something. At some point in the code, you'd like to log some information and include the kv pairs in the log as k=val (tagged data).

info!("some logging state={}", state, something);
or if its easier:
info!("some logging state={}", state, &something);
or if its easier:
info!("some logging state={}", state, Some(something));

or if easier, the log_context field could be an Option<_>

Reference-level explanation

This is another means of specifying kv pairs, and would likely require new macros, e.g., info_kv!(...) or reworking existing macros.

Drawbacks

This could be work that few take advantage of. Although, I suspect once the community starts seeing it in action adoption may occur rapidly.

Rationale and alternatives

The primary alternative is to forgo the macros and build the Record using builder methods, which is at best tedious.

Unresolved Questions

I don't believe this creates unresolved issues.

Not printing the key value pairs when using log4rs

Bug Report

Your Environment

| Software | Version(s) |
| MacOS 11.04 |
| kv-log-macro 1.0.7 |
| Rustc - rustc 1.53.0-nightly (673d0db5e 2021-03-23 |

Expected Behavior

The last line of the example is:

info!("hello {}", "cats", {
            cat_1: "chashu",
            cat_2: "nori",
        });

I'm expecting this to print key of cat_1 and a value of cats etc.

{"message":"hello cats",level":"INFO", "cat_1": "chahu", ... }

Current Behavior

(removed irrelevant fields)

{"message":"hello cats",level":"INFO", ... }

Nothing related to the key value fields are logged at all.

Code Sample

The code:

use kv_log_macro::info;

#[tokio::main]
async fn main() {
    log4rs::init_file("./config/log4rs.yml", Default::default())
        .expect("Should have logging configuration at config/log4rs.yml");
    info!("hello {}", "cats", {
            cat_1: "chashu",
            cat_2: "nori",
        });
}

Log4rs config:

refresh_rate: 30 seconds

appenders:
  json_file:
    kind: file
    path: 'json.log'
    encoder:
      kind: json

  human_file:
    kind: file
    path: "human.log"
    encoder:
      pattern: "{h({d} {l} - {m}{n})}"

root:
  level: info
  appenders:
    - json_file
    - human_file

Missing log output

I don't know where in the chain this breaks, but when using femme as a logger with this only log::info!("hello", {}) works, but log::info!("hello") gets ignored.

Unable to use `log_enabled`

use kv_log_macro::{log_enabled, level::Debug};

if log_enabled!(Debug) {
  // DO THINGs
}

Fails to compile

error[E0425]: cannot find function `__private_api_enabled` in crate `$crate`
   --> src/log/middleware.rs:70:16
    |
70  |               if log_enabled!(Debug) {
    |                  ^^^^^^^^^^^^^^^^^^^ help: a function with a similar name exists: `__private_api_log`
    | 
   ::: /Users/dignifiedquire/.cargo/registry/src/github.com-1ecc6299db9ec823/kv-log-macro-1.0.4/src/lib.rs:228:1
    |
228 | / pub fn __private_api_log(
229 | |     args: fmt::Arguments<'_>,
230 | |     level: Level,
231 | |     &(target, module_path, file, line): &(&str, &'static str, &'static str, u32),
...   |
244 | |     );
245 | | }
    | |_- similarly named function `__private_api_log` defined here
    |
    = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.