Giter Club home page Giter Club logo

json_logger's Introduction

Elixir JSON Logger

JSON Logger is a logger backend that outputs elixir logs in JSON format.

This project is originally designed to make Elixir apps work with Logstash easily. It aims at providing as much information for the log is possible, so the logs can be more easily analyzed by backend services like Elasticsearch.

Issues and PRs are welcome.

Dependencies

This project requires json.

Configuration

Elixir Project

JSON Logger currently provides very few options:

  • level: The minimal level of logging. There's no default of this option. Example: level: :warn
  • output: The output of the log. Must be either :console or {:udp, host, port} or {:tcp, host, port}. Example: output: {:udp, "localhost", 514}`
  • metadata: Whatever else you want in the log. Example: metadata: "Some very important project"

Example configuration: config :logger, :json_logger, level: :info, output: {:udp, "localhost", 514}

TCP support is still experimental, please submit issues that you encounter.

In your application

You should add json_logger to your mix.exs as well. This step may not be necessary (if you know why please tell me).

defmodule MyMod.Mixfile do
  # ...
  def application do
    [applications: [:logger, :json_logger],
     mod: {MyMod, []}]
  end
  # ...
end

Adding the logger backend

You need to add this backend to your Logger, preferably put this in your Application's start/2.

Logger.add_backend Logger.Backends.JSON

If you wish to use Logstash with this library

Here is an example logstash configuration:

input {
  udp {
    port => 514
    type => "elixir_json_logging"
  }
}

filter {
  json {
    source => "message"
  }
}

output {
  stdout {
    codec => rubydebug
  }
}

Note that this configuration will probably break on your system (listening to a <1024 port). You should change the "port" to a larger value.

json_logger's People

Contributors

leeroyding avatar semmelk avatar sztheory avatar tanguyantoine avatar

Stargazers

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

Watchers

 avatar  avatar

json_logger's Issues

Occasional failures of test "can use info level"

Sometimes when running tests, the named test would fail while others don't.

The failure looks like:

   1) test can use info level (Logger.Backends.JSON.UDPTest)
      test/json_logger_udp_test.exs:42
      ** (exit) exited in: GenEvent.call(Logger, {Logger.Backends.JSON, :test}, {:configure, [level: :info]}, 5000)
          ** (EXIT) no process
      stacktrace:
        (elixir) lib/gen_event.ex:561: GenEvent.call/4
        test/json_logger_udp_test.exs:43

Configurable JSON library

Hey,

Any chance you'd welcome a PR making the JSON library configurable. Many (I dare say most) elixir developers are using Poison, and it would be nice to not need an additional JSON parser in our dependency tree.

Thanks!

Phoenix error

I have this error:

(FunctionClauseError) no function clause matching in Logger.Backends.JSON.event_json/5
        (json_logger) lib/json_logger.ex:90: Logger.Backends.JSON.event_json(:error,

(seems to be recursive)

my conf :

config :logger, :console, format: "[$level] $metadata - $message\n"
config :logger, :json_logger, level: :info, output: :console

i added to def start/2

Logger.add_backend Logger.Backends.JSON    

Any idea?

Test failure on Elixir 1.2.1 / OTP 18

All four tests fail on my machine:

$ elixir --version
Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Elixir 1.2.1
$ mix test

=INFO REPORT==== 20-Jan-2016::23:18:36 ===
    application: logger
    exited: shutdown
    type: temporary


  1) test sends debug message via UDP (Logger.Backends.JSON.UDPTest)
     test/json_logger_udp_test.exs:21
     match (=) failed
     code: {:ok, {_ip, _port, message}} = :gen_udp.recv(server, 0, 500)
     rhs:  {:error, :timeout}
     stacktrace:
       test/json_logger_udp_test.exs:23



  2) test can change metadata (Logger.Backends.JSON.UDPTest)
     test/json_logger_udp_test.exs:30
     ** (exit) exited in: GenEvent.call(Logger, {Logger.Backends.JSON, :test}, {:configure, [metadata: "New metadata"]}, 5000)
         ** (EXIT) no process
     stacktrace:
       (elixir) lib/gen_event.ex:561: GenEvent.call/4
       test/json_logger_udp_test.exs:32



  3) test can use info level (Logger.Backends.JSON.UDPTest)
     test/json_logger_udp_test.exs:42
     ** (exit) exited in: GenEvent.call(Logger, {Logger.Backends.JSON, :test}, {:configure, [level: :info]}, 5000)
         ** (EXIT) no process
     stacktrace:
       (elixir) lib/gen_event.ex:561: GenEvent.call/4
       test/json_logger_udp_test.exs:43

?

  4) Logger.Backends.JSON.TCPTest: failure on setup_all callback, tests invalidated
     ** (exit) exited in: GenEvent.call(Logger, {Logger.Backends.JSON, :test}, {:configure, [level: :debug, metadata: "Very important data", output: {:tcp, "localhost", 38206}]}, 5000)
         ** (EXIT) no process
     stacktrace:
       (elixir) lib/gen_event.ex:561: GenEvent.call/4
       test/json_logger_tcp_test.exs:14: Logger.Backends.JSON.TCPTest.__ex_unit_setup_all_0/1
       test/json_logger_tcp_test.exs:1: Logger.Backends.JSON.TCPTest.__ex_unit__/2



Finished in 0.5 seconds (0.08s on load, 0.5s on tests)
4 tests, 4 failures, 1 invalid

Randomized with seed 594801

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.