Óg is a small collection of debugging functions for logging data.
- Functions
Og.log/2andOg.log_r/2are primarily intended as debugging tools.
Add óg to your list of dependencies in mix.exs:
def deps, do: [{:og, "~> 1.0"}]Ensure that :logger is started in the applications:
def application do [applications: [:logger, :og]] end-
log/2- logs the data transformed by the inspector function and returns:ok -
log_r/2- logs the data transformed by the inspector function and returns the original data. -
Inspection of the data before logging it can be helpful in a debugging context for example for
- Avoiding the
Protocol.UndefinedErrorwhen logging tuples. - Not needing to require Logger
- Avoiding the
-
Formatting with
Kernel.inspecton the data carries an overhead soOg.log/2andOg.log_r/2should be probably be reserved for debugging code in:devenvironments.
config :logger, :og,
kernel_opts: [width: 70],
apex_opts: [numbers: :false, color: :false],
sanitize_by_default: :false,
default_inspector: :kernel
-
kernel_opts- corresponds to elixir inspect opts forIO.inspect/2andKernel.inspect/2, refer tohttps://hexdocs.pm/elixir/Inspect.Opts.html -
apex_opts- corresponds to options for theApex.Format.format/2function, refer tohttps://github.com/BjRo/apex/blob/master/lib/apex/format.ex -
sanitize_by_default- defaults to:false, when set to:true, an attempt will be made to apply theSecureLogFormatter.sanitize/1function on the data before applying the inspection function. For this function to take any effect, the settings forSecureLogFormattermust also be placed inconfig.exs. See the following secure_log_formatter url for more details. -
default_inspector- corresponds to the default inspector which will apply to the data passed to the log function. This can be overriden in the options of the log function. The options are:kernelor:apex, the default is:kernel.
Example configuration for secure_log_formatter, as referenced at the following secure_log_formatter url.
config :logger,
secure_log_formatter:
[
# Map and Keyword List keys who's value should be hidden
fields: ["password", "credit_card", ~r/.*_token/],
# Patterns which if found, should be hidden
patterns: [~r/4[0-9]{15}/] # Simple credit card example
# defaults to "[REDACTED]"
replacement: "[PRIVATE]"
]
- Basic logging
Og.log(:this_is_a_test)- Logging at the
:warnlevel
Og.log(:this_is_a_test, level: :warn)- Logging at the
:warnlevel and with ENV specified to get richer information
Og.log(:this_is_a_test, level: :warn, env: __ENV__)- Logging with the Apex inspector
Og.log(:this_is_a_test, inspector: :apex)- Logging and applying sanitization with the features of secure_log_formatter
Og.log(%{credit_card: 4111111111111}, sanitize: :true)- Logging inside a chain of piped functions
defmodule OgTest do
def log() do
%{first: "john", last: "doe"}
|> Map.to_list()
|> Enum.filter( &(&1 === {:first, "john"}))
|> Og.log_r()
|> List.last()
|> Tuple.to_list()
|> List.last()
|> Og.log_r(env: __ENV__, inspector: :kernel, level: :info)
|> String.upcase()
end
end
OgTest.log()-
- Setting the
config.exsopts or the log function opts toinspector: :apexwill use theApex.Format.format/2function from the apex library.
- Setting the
-
SecureLogFormatter library --- Sean Callan
- Setting
config.exsopts or the log function opts tosanitize: :truewill use theSecureLogFormatter.sanitize/1function from the SecureLogFormatter library.
- Setting
- Investigate adding a custom formatting module as an optional additional means of logging.
MIT