您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

104 行
3.4 KiB

  1. %% Copyright (c) 2011 Basho Technologies, Inc. All Rights Reserved.
  2. %%
  3. %% This file is provided to you under the Apache License,
  4. %% Version 2.0 (the "License"); you may not use this file
  5. %% except in compliance with the License. You may obtain
  6. %% a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing,
  11. %% software distributed under the License is distributed on an
  12. %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. %% KIND, either express or implied. See the License for the
  14. %% specific language governing permissions and limitations
  15. %% under the License.
  16. -module(lager_util).
  17. -include_lib("kernel/include/file.hrl").
  18. -export([levels/0, level_to_num/1, num_to_level/1, open_logfile/2,
  19. ensure_logfile/4, format_time/0, format_time/1]).
  20. levels() ->
  21. [debug, info, notice, warning, error, critical, alert, emergency].
  22. level_to_num(debug) -> 0;
  23. level_to_num(info) -> 1;
  24. level_to_num(notice) -> 2;
  25. level_to_num(warning) -> 3;
  26. level_to_num(error) -> 4;
  27. level_to_num(critical) -> 5;
  28. level_to_num(alert) -> 6;
  29. level_to_num(emergency) -> 7.
  30. num_to_level(0) -> debug;
  31. num_to_level(1) -> info;
  32. num_to_level(2) -> notice;
  33. num_to_level(3) -> warning;
  34. num_to_level(4) -> error;
  35. num_to_level(5) -> critical;
  36. num_to_level(6) -> alert;
  37. num_to_level(7) -> emergency.
  38. open_logfile(Name, Buffer) ->
  39. case filelib:ensure_dir(Name) of
  40. ok ->
  41. Options = [append, raw] ++
  42. if Buffer == true -> [delayed_write];
  43. true -> []
  44. end,
  45. case file:open(Name, Options) of
  46. {ok, FD} ->
  47. case file:read_file_info(Name) of
  48. {ok, FInfo} ->
  49. Inode = FInfo#file_info.inode,
  50. {ok, {FD, Inode}};
  51. X -> X
  52. end;
  53. Y -> Y
  54. end;
  55. Z -> Z
  56. end.
  57. ensure_logfile(Name, FD, Inode, Buffer) ->
  58. case file:read_file_info(Name) of
  59. {ok, FInfo} ->
  60. Inode2 = FInfo#file_info.inode,
  61. case Inode == Inode2 of
  62. true ->
  63. {ok, {FD, Inode}};
  64. false ->
  65. %% delayed write can cause file:close not to do a close
  66. file:close(FD),
  67. file:close(FD),
  68. case open_logfile(Name, Buffer) of
  69. {ok, {FD2, Inode3}} ->
  70. %% inode changed, file was probably moved and
  71. %% recreated
  72. {ok, {FD2, Inode3}};
  73. Error ->
  74. Error
  75. end
  76. end;
  77. _ ->
  78. %% delayed write can cause file:close not to do a close
  79. file:close(FD),
  80. file:close(FD),
  81. case open_logfile(Name, Buffer) of
  82. {ok, {FD2, Inode3}} ->
  83. %% file was removed
  84. {ok, {FD2, Inode3}};
  85. Error ->
  86. Error
  87. end
  88. end.
  89. format_time() ->
  90. format_time(lager_stdlib:maybe_utc(erlang:localtime())).
  91. format_time({utc, {{Y, M, D}, {H, Mi, S}}}) ->
  92. io_lib:format("~b-~2..0b-~2..0b ~2..0b:~2..0b:~2..0b UTC", [Y, M, D, H, Mi, S]);
  93. format_time({{Y, M, D}, {H, Mi, S}}) ->
  94. io_lib:format("~b-~2..0b-~2..0b ~2..0b:~2..0b:~2..0b", [Y, M, D, H, Mi, S]).