%% -*- erlang -*-
%% complex lager example
%% @doc where do you want the console.log output:
%% off : nowhere
%% file: the file specified by log.console.file
%% console : standard out
%% both : log.console.file and standard out.
{mapping, "log.console", "lager.handlers", [
  {default, file},
  {datatype, {enum, [off, file, console, both]}}
]}.

%% @doc the log level of the console log
{mapping, "log.console.level", "lager.handlers", [
  {default, info},
  {datatype, {enum, [debug, info, warning, error]}}
]}.

%% @doc location of the console log
{mapping, "log.console.file", "lager.handlers", [
  {default, "{{log_path}}/console.log"}
]}.

%% *gasp* notice the same @mapping!
%% @doc location of the error log
{mapping, "log.error.file", "lager.handlers", [
  {default, "{{log_path}}/error.log"}
]}.

%% *gasp* notice the same @mapping!
%% @doc location of the debug log
{mapping, "log.debug.file", "lager.handlers", [
  {default, "{{log_path}}/debug.log"}
]}.

%% *gasp* notice the same @mapping!
%% @doc turn on syslog
{mapping, "log.syslog", "lager.handlers", [
  {default, off},
  {datatype, {enum, [on, off]}}
]}.

%% @doc the log level of the watchdog log
{mapping, "log.watchdog.level", "lager.handlers", [
  {default, error},
  {datatype, {enum, [debug, info, warning, error]}}]}.

%% @doc the log level of the watchdog log
{mapping, "log.watchdog.cluster", "lager.handlers", [
  {default, "fifo"},
  {datatype, string}]}.

%% @doc the log level of the watchdog log
{mapping, "log.watchdog.service", "lager.handlers", [
  {default, "{{watchdog_service}}"},
  {datatype, string}]}.

%% @doc The ip of the watchdog server
{mapping, "log.watchdog.host.$name", "lager.handlers",
 [{commented, {"127.0.0.1", 4444 }},
  {datatype, ip}]}.

{ translation,
  "lager.handlers",
  fun(Conf) ->
    SyslogHandler = case cuttlefish:conf_get("log.syslog", Conf) of
      on ->  [{lager_syslog_backend, ["riak", daemon, info]}];
      _ -> []
    end,
    ErrorHandler = case cuttlefish:conf_get("log.error.file", Conf) of
      undefined -> [];
      ErrorFilename -> [{lager_file_backend, [{file, ErrorFilename},
                                              {level, error},
                                              {size, 10485760},
                                              {date, "$D0"},
                                              {count, 5}]}]
    end,

    WatchdogHandler = case cuttlefish_util:filter_by_variable_starts_with("log.watchdog.host", Conf) of
      undefined -> [];
      [] -> [];
      WatchdogLogServers ->
        Servers1 = [S || {_Name, S} <- WatchdogLogServers],
        WatchdogLogLevel = cuttlefish:conf_get("log.watchdog.level", Conf),
        WDCluster = cuttlefish:conf_get("log.watchdog.cluster", Conf),
        WDService = cuttlefish:conf_get("log.watchdog.service", Conf),
        [{lager_watchdog, [WDCluster, WDService, WatchdogLogLevel, Servers1]}]
    end,

    ConsoleLogLevel = cuttlefish:conf_get("log.console.level", Conf),
    ConsoleLogFile = cuttlefish:conf_get("log.console.file", Conf),

    ConsoleHandler = {lager_console_handler, ConsoleLogLevel},
    ConsoleFileHandler = {lager_file_backend, [{file, ConsoleLogFile},
                                                {level, ConsoleLogLevel},
                                                {size, 10485760},
                                                {date, "$D0"},
                                                {count, 5}]},

    ConsoleHandlers = case cuttlefish:conf_get("log.console", Conf) of
      off -> [];
      file -> [ConsoleFileHandler];
      console -> [ConsoleHandler];
      both -> [ConsoleHandler, ConsoleFileHandler];
      _ -> []
    end,
    DebugHandler = case cuttlefish:conf_get("log.debug.file", Conf) of
      undefined -> [];
      DebugFilename -> [{lager_file_backend, [{file, DebugFilename},
                                              {level, debug},
                                              {size, 10485760},
                                              {date, "$D0"},
                                              {count, 5}]}]
    end,

    SyslogHandler ++ ConsoleHandlers ++ ErrorHandler ++ DebugHandler ++ WatchdogHandler
  end
}.

%% Lager Config

%% @doc Whether to write a crash log, and where.
%% Commented/omitted/undefined means no crash logger.
{mapping, "log.crash.file", "lager.crash_log", [
  {default, "{{log_path}}/crash.log"}
]}.

%% @doc Maximum size in bytes of events in the crash log - defaults to 65536
%% @datatype integer
%% @mapping
{mapping, "log.crash.msg_size", "lager.crash_log_msg_size", [
  {default, "64KB"},
  {datatype, bytesize}
]}.

%% @doc Maximum size of the crash log in bytes, before its rotated, set
%% to 0 to disable rotation - default is 0
{mapping, "log.crash.size", "lager.crash_log_size", [
  {default, "10MB"},
  {datatype, bytesize}
]}.

%% @doc What time to rotate the crash log - default is no time
%% rotation. See the lager README for a description of this format:
%% https://github.com/basho/lager/blob/master/README.org
{mapping, "log.crash.date", "lager.crash_log_date", [
  {default, "$D0"}
]}.

%% @doc Number of rotated crash logs to keep, 0 means keep only the
%% current one - default is 0
{mapping, "log.crash.count", "lager.crash_log_count", [
  {default, 5},
  {datatype, integer}
]}.

%% @doc Whether to redirect error_logger messages into lager - defaults to true
{mapping, "log.error.redirect", "lager.error_logger_redirect", [
  {default, on},
  {datatype, {enum, [on, off]}}
]}.

{ translation,
  "lager.error_logger_redirect", fun(Conf) ->
    Setting = cuttlefish:conf_get("log.error.redirect", Conf),
    case Setting of
      on -> true;
      off -> false;
      _Default -> true
    end
end}.

%% @doc maximum number of error_logger messages to handle in a second
%% lager 2.0.0 shipped with a limit of 50, which is a little low for riak's startup
{mapping, "log.error.messages_per_second", "lager.error_logger_hwm", [
  {default, 100},
  {datatype, integer}
]}.

%% SASL
%% We should never care about this
{mapping, "sasl", "sasl.sasl_error_logger", [
  {default, off},
  {datatype, {enum, [on, off]}},
  {level, advanced}
]}.

{ translation,
  "sasl.sasl_error_logger",
  fun(Conf) ->
    case cuttlefish:conf_get("sasl", Conf) of %%how to pull default?
        on -> true;
        _ -> false
    end
  end
}.
