Skip to content

Instantly share code, notes, and snippets.

@lpgauth
Created August 30, 2012 18:33
Show Gist options
  • Save lpgauth/3536889 to your computer and use it in GitHub Desktop.
Save lpgauth/3536889 to your computer and use it in GitHub Desktop.
log_event(Message, TS) when is_binary(Message) ->
log_event(binary_to_list(Message), TS);
log_event(Message, TS) when is_list(Message) ->
{{Y, M, D}, {H, _M, _S}} = calendar:now_to_universal_time(TS),
Name = {Y, M, D, H},
case disk_log:balog(Name, unicode:characters_to_binary([Message, "\n"])) of
ok ->
ok;
{error, no_such_log} ->
{ok, Path} = application:get_env(adgear_delivery, log_path),
Filename = io_lib:format("~s/~4..0b-~2..0b-~2..0b-~2..0b.log", [Path, Y, M, D, H]),
case disk_log:open([{name, {Y, M, D, H}}, {file, Filename}, {type, halt}, {format, external}, {mode, read_write}, {head, none}]) of
{ok, _NewName} ->
{{Y2, M2, D2}, {H2, _M2, _S2}} = calendar:gregorian_seconds_to_datetime(calendar:datetime_to_gregorian_seconds(calendar:universal_time())-3600),
disk_log:close({Y2, M2, D2, H2}),
disk_log:balog(Name, unicode:characters_to_binary(Message ++ "\n")),
ok;
{error, Error} ->
{error, Error}
end;
{error, Error} ->
{error, Error}
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment