logger_json v3.0.2 LoggerJSON

JSON console back-end for Elixir Logger.

It can be used as drop-in replacement for default :console Logger back-end in cases where you use use Google Cloud Logger or other JSON-based log collectors.

Log Format

By-default, generated JSON is compatible with Google Cloud Logger LogEntry format:

  {
    "log":"hello",
    "logging.googleapis.com/sourceLocation":{
      "file":"/os/logger_json/test/unit/logger_json_test.exs",
      "function":"Elixir.LoggerJSONTest.test metadata can be configured/1",
      "line":71
    },
    "severity":"DEBUG",
    "time":"2018-10-19T01:10:49.582Z",
    "user_id":13
  }

You can change this structure by implementing LoggerJSON.Formatter behaviour and passing module name to :formatter config option. Example module can be found in LoggerJSON.Formatters.GoogleCloudLogger.

  config :logger_json, :backend,
    formatter: MyFormatterImplementation

Encoders support

You can replace default Jason encoder with other module that supports encode!/1 function. This can be even used as custom formatter callback.

Popular Jason alternatives:

Dynamic configuration

For dynamically configuring the endpoint, such as loading data from environment variables or configuration files, LoggerJSON provides an :on_init option that allows developers to set a module, function and list of arguments that is invoked when the endpoint starts.

  config :logger_json, :backend,
    on_init: {YourApp.Logger, :load_from_system_env, []}

Link to this section Summary

Functions

Callback implementation for c::gen_event.code_change/3

Changes Logger log level at runtime

Configures Logger log level at runtime by using value from environment variable

Callback implementation for c::gen_event.handle_call/2

Callback implementation for c::gen_event.handle_event/2

Callback implementation for c::gen_event.handle_info/2

Callback implementation for c::gen_event.init/1

Callback implementation for c::gen_event.terminate/2

Link to this section Functions

Link to this function

code_change(old_vsn, state, extra)

Callback implementation for c::gen_event.code_change/3.

Link to this function

configure_log_level!(level)

Changes Logger log level at runtime.

Notice that settings this valie below compile_time_purge_level would not work, because Logger calls would be already stripped at compile-time.

Link to this function

configure_log_level_from_env!(env_name \\ "LOG_LEVEL")

Configures Logger log level at runtime by using value from environment variable.

By default, 'LOG_LEVEL' environment variable is used.

Link to this function

handle_call(arg, state)

Callback implementation for c::gen_event.handle_call/2.

Link to this function

handle_event(arg1, state)

Callback implementation for c::gen_event.handle_event/2.

Link to this function

handle_info(arg1, state)

Callback implementation for c::gen_event.handle_info/2.

Callback implementation for c::gen_event.init/1.

Link to this function

take_metadata(metadata, keys_or_all, ignored_keys \\ [])

Link to this function

terminate(reason, state)

Callback implementation for c::gen_event.terminate/2.