-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: accepts LOGGING_BACKEND=INK for json logging (#1352)
* use Ink for multiline logs in datadog, expand Sentry tags * default Ink module attr * default Ink module attr * set logger in every application * new line and dots
- Loading branch information
Ino Murko
authored
Feb 26, 2020
1 parent
1b5c2ec
commit fb4742b
Showing
13 changed files
with
191 additions
and
58 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
apps/omg_status/lib/omg_status/release_tasks/set_logger.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright 2019-2019 OmiseGO Pte Ltd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
defmodule OMG.Status.ReleaseTasks.SetLogger do | ||
@moduledoc false | ||
use Distillery.Releases.Config.Provider | ||
require Logger | ||
|
||
@app :logger | ||
@default_backend Ink | ||
|
||
@impl Provider | ||
def init(_) do | ||
logger_backends = Application.get_env(@app, :backends, persistent: true) | ||
logger_backend = get_logger_backend() | ||
|
||
remove = | ||
case logger_backend do | ||
:console -> Ink | ||
_ -> :console | ||
end | ||
|
||
backends = logger_backends |> Kernel.--([remove]) |> Enum.concat([logger_backend]) |> Enum.uniq() | ||
:ok = Application.put_env(@app, :backends, backends, persistent: true) | ||
end | ||
|
||
defp get_logger_backend() do | ||
logger = | ||
"LOGGER_BACKEND" | ||
|> get_env() | ||
|> validate_string(@default_backend) | ||
|
||
_ = Logger.info("CONFIGURATION: App: #{@app} Key: LOGGER_BACKEND Value: #{inspect(logger)}.") | ||
logger | ||
end | ||
|
||
defp get_env(key), do: System.get_env(key) | ||
|
||
defp validate_string(nil, default), do: default | ||
defp validate_string(value, default), do: do_validate_string(String.upcase(value), default) | ||
defp do_validate_string("CONSOLE", _default), do: :console | ||
defp do_validate_string("INK", _default), do: Ink | ||
defp do_validate_string(_, default), do: default | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
apps/omg_status/test/omg_status/release_tasks/set_logger_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Copyright 2019-2020 OmiseGO Pte Ltd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
defmodule OMG.Status.ReleaseTasks.SetLoggerTest do | ||
use ExUnit.Case, async: false | ||
alias OMG.Status.ReleaseTasks.SetLogger | ||
@app :logger | ||
setup_all do | ||
logger_backends = Application.get_env(@app, :backends, persistent: true) | ||
|
||
on_exit(fn -> | ||
:ok = Application.put_env(@app, :backends, logger_backends) | ||
:ok = System.delete_env("LOGGER_BACKEND") | ||
end) | ||
|
||
:ok | ||
end | ||
|
||
setup do | ||
:ok = System.delete_env("LOGGER_BACKEND") | ||
end | ||
|
||
test "if environment variables (INK) get applied in the configuration" do | ||
:ok = System.put_env("LOGGER_BACKEND", "INK") | ||
:ok = SetLogger.init([]) | ||
configuration = Application.get_env(@app, :backends) | ||
assert Enum.member?(configuration, Ink) == true | ||
end | ||
|
||
test "if environment variables (CONSOLE) get applied in the configuration" do | ||
# env var to console and asserting that Ink gets removed | ||
:ok = System.put_env("LOGGER_BACKEND", "conSole") | ||
:ok = SetLogger.init([]) | ||
configuration = Application.get_env(@app, :backends) | ||
assert Enum.member?(configuration, :console) == true | ||
assert Enum.member?(configuration, Ink) == false | ||
end | ||
|
||
test "if environment variables are not present the default configuration gets used (INK)" do | ||
# in mix_env == test the default logger is :console and Sentry.LoggerBackend | ||
# we want to test our production default setting which is Ink and Sentry.LoggerBackend | ||
# so we modify the configuration first so that it looks like in mix env prod | ||
Application.put_env(@app, :backends, [Ink, Sentry.LoggerBackend], persistent: true) | ||
# we continue with setting the backend to console and asserting that Ink gets removed | ||
:ok = SetLogger.init([]) | ||
configuration = Application.get_env(@app, :backends) | ||
assert Enum.member?(configuration, :console) == false | ||
assert Enum.member?(configuration, Ink) == true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
use Mix.Config | ||
|
||
config :logger, | ||
backends: [:console, Sentry.LoggerBackend] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.