Skip to content

Commit

Permalink
Stop logging to slack when level not in configured levels
Browse files Browse the repository at this point in the history
  • Loading branch information
craigp committed Mar 8, 2016
1 parent 17b6a37 commit 8fcf849
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 55 deletions.
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
erl_crash.dump
*.ez
*.beam
dev.exs
test.local.exs
config/dev.exs
config/test.local.exs
.envrc
doc
tags
Expand Down
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,21 @@ def application do
end
```

You'll need to create a custom incoming webhook URL for your Slack team. Check the `config/dev.exs` file
for a configuration example. You can also put the webhook URL in the `SLACK_LOGGER_WEBHOOK_URL` environment
variable if you prefer.
You can set the log levels you want posted to slack in the config:

```elixir
config :slack_logger_backend, :levels, [:debug, :info, :warn, :error]
```

You'll need to create a custom incoming webhook URL for your Slack team. You can either configure the webhook
in your config:

```elixir
config :slack_logger_backend, :slack, [url: "http://example.com"]
```

... or you can put the webhook URL in the `SLACK_LOGGER_WEBHOOK_URL` environment variable if you prefer. If
you have both the environment variable will be preferred.

### TODO

Expand Down
28 changes: 0 additions & 28 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,33 +1,5 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for
# 3rd-party users, it should be done in your "mix.exs" file.

# You can configure for your application as:
#
# config :slack_logger_backend, key: :value
#
# And access this configuration in your application as:
#
# Application.get_env(:slack_logger_backend, :key)
#
# Or configure a 3rd-party app:
#
# config :logger, level: :info
#

# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env}.exs"
path = __DIR__ |> Path.expand |> Path.join("#{Mix.env}.exs")
if File.exists?(path), do: import_config "#{Mix.env}.exs"

4 changes: 4 additions & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use Mix.Config

config :slack_logger_backend, :levels, [:debug, :info, :warn, :error]
config :slack_logger_backend, :slack, [url: "http://example.com"]
16 changes: 8 additions & 8 deletions lib/slack_logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ defmodule SlackLogger do

@doc false
def init(__MODULE__) do
levels = case Application.get_env(:slack_logger_backend, :levels) do
nil ->
[:error] # by default only log error level messages
levels ->
levels
end
{:ok, %{levels: levels}}
{:ok, %{}}
end

@doc false
Expand All @@ -25,7 +19,13 @@ defmodule SlackLogger do
end

@doc false
def handle_event({level, _pid, {Logger, message, _timestamp, detail}}, %{levels: levels} = state) do
def handle_event({level, _pid, {Logger, message, _timestamp, detail}}, state) do
levels = case Application.get_env(:slack_logger_backend, :levels) do
nil ->
[:error] # by default only log error level messages
levels ->
levels
end
if level in levels do
handle_event(level, message, detail)
end
Expand Down
29 changes: 19 additions & 10 deletions lib/slack_logger_backend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,35 @@ defmodule SlackLoggerBackend do
```elixir
def deps do
[{:slack_logger_backend, "~> 0.1"}]
[{:slack_logger_backend, "~> 0.0.1"}]
end
```
Then run `$ mix do deps.get, compile` to download and compile
your dependencies.
Then run `$ mix do deps.get, compile` to download and compile your dependencies.
Finally, add the `:slack_logger_backend` application as your
list of applications in `mix.exs`:
Finally, add the `:slack_logger_backend` application as your list of applications in `mix.exs`:
```elixir
def application do
[applications: [:logger, :slack_logger_backend]]
[applications: [:logger, :slack_logger_backend]]
end
```
You'll need to create a custom incoming webhook URL for your
Slack team. Check the `config/dev.exs` file for a configuration
example. You can also put the webhook URL in the
`SLACK_LOGGER_WEBHOOK_URL` environment variable if you prefer.
You can set the log levels you want posted to slack in the config:
```elixir
config :slack_logger_backend, :levels, [:debug, :info, :warn, :error]
```
You'll need to create a custom incoming webhook URL for your Slack team. You can either configure the webhook
in your config:
```elixir
config :slack_logger_backend, :slack, [url: "http://example.com"]
```
... or you can put the webhook URL in the `SLACK_LOGGER_WEBHOOK_URL` environment variable if you prefer. If
you have both the environment variable will be preferred.
"""

use Application
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule SlackLoggerBackend.Mixfile do
[
app: :slack_logger_backend,
description: "A logger backend for posting errors to Slack.",
version: "0.1.2",
version: "0.1.3",
elixir: "~> 1.2",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
Expand Down
8 changes: 7 additions & 1 deletion test/slack_logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ defmodule SlackLoggerTest do
Plug.Conn.resp(conn, 200, "ok")
end
Logger.error "This error should be logged to Slack"
:timer.sleep(200)
:timer.sleep(200) # this is terrible but i need to wait for bypass
end

test "doesn't post a debug message to Slack if the level is not set" do
Application.put_env :slack_logger_backend, :levels, [:info]
Logger.error "This error should not be logged to Slack"
:timer.sleep(200) # this is terrible but i need to wait for bypass
end

end

0 comments on commit 8fcf849

Please sign in to comment.