You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by davisp <gi...@git.apache.org> on 2016/07/20 03:53:29 UTC

[GitHub] couchdb-couch-log pull request #13: Make couch_log smarter

GitHub user davisp opened a pull request:

    https://github.com/apache/couchdb-couch-log/pull/13

    Make couch_log smarter

    This drops the lager and goldrush dependencies and instead moves all
    logic to couch_log so that we can make our logging work more closely
    with the existing configuration system.
    
    COUCHDB-3067
    
    ---
    
    Don't be overly alarmed at the size of this diff. A large portion of it is pulled in from lager for trunc_io support along with an io_lib:format implementation that uses said trunc_io. Other than that  its mostly just a clean up of the current couch_log app with some added features like logging to files.
    
    I've only done minimal testing on this so far. It passes `make javascript` with the stderr logger and I can change log levels during a test run. There are a few log lines that need cleaned up that we can do with other PRs. Mostly its just log lines that include a trailing new line when the shouldn't. There's also a log from the emulator that does the same that I want to track down to remove the trailing newline.
    
    This has stderr and file backends. Tomorrow I'm gonna add a syslog backend. After that it'll be all about adding whatever tests people think are necessary. There really isn't a shit load of testing around logging (given, if it were broke I think we'd know quickly). However with the current implementation we should be able get fairly deep testing coverage quickly if people actually want to push on it.
    
    For the moment this is just for review, I want to get the stderr implementation working before we actually merge and also obviously address any feedback to this PR.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/cloudant/couchdb-couch-log 3067-improve-couch-log

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/couchdb-couch-log/pull/13.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #13
    
----
commit fa16ed428adeee4b3c5fbb714ffee7f8f4b8a83d
Author: Paul J. Davis <pa...@gmail.com>
Date:   2016-07-20T00:14:33Z

    Make couch_log smarter
    
    This drops the lager and goldrush dependencies and instead moves all
    logic to couch_log so that we can make our logging work more closely
    with the existing configuration system.
    
    COUCHDB-3067

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-log pull request #13: Make couch_log smarter

Posted by rnewson <gi...@git.apache.org>.
Github user rnewson commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-log/pull/13#discussion_r71570705
  
    --- Diff: src/couch_log_monitor.erl ---
    @@ -0,0 +1,68 @@
    +% Copyright 2011 Cloudant
    --- End diff --
    
    2016


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-log pull request #13: Make couch_log smarter

Posted by rnewson <gi...@git.apache.org>.
Github user rnewson commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-log/pull/13#discussion_r71570963
  
    --- Diff: src/couch_log_util.erl ---
    @@ -0,0 +1,145 @@
    +% 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.
    +
    +-module(couch_log_util).
    +
    --- End diff --
    
    vsn


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-log pull request #13: Make couch_log smarter

Posted by davisp <gi...@git.apache.org>.
Github user davisp commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-log/pull/13#discussion_r71574030
  
    --- Diff: src/couch_log_writer_file.erl ---
    @@ -0,0 +1,124 @@
    +% 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.
    +
    +-module(couch_log_writer_file).
    +-behaviour(couch_log_writer).
    +
    +
    +-export([
    +    init/0,
    +    terminate/2,
    +    write/2
    +]).
    +
    +
    +-include_lib("kernel/include/file.hrl").
    +-include("couch_log.hrl").
    +
    +
    +-record(st, {
    +    file_path,
    +    fd,
    +    inode,
    +    last_check
    +}).
    +
    +
    +-define(CHECK_INTERVAL, 30000000).
    +
    +
    +init() ->
    +    FilePath = config:get("log", "file", "./couch.log"),
    +    Opts = [append, raw] ++ buffer_opt(),
    +    case filelib:ensure_dir(FilePath) of
    +        ok ->
    +            case file:open(FilePath, Opts) of
    +                {ok, Fd} ->
    +                    case file:read_file_info(FilePath) of
    +                        {ok, FInfo} ->
    +                            {ok, #st{
    +                                file_path = FilePath,
    +                                fd = Fd,
    +                                inode = FInfo#file_info.inode,
    +                                last_check = os:timestamp()
    +                            }};
    +                        FInfoError ->
    +                            FInfoError
    +                    end;
    +                OpenError ->
    +                    OpenError
    +            end;
    +        EnsureDirError ->
    +            EnsureDirError
    +    end.
    +
    +
    +terminate(_, St) ->
    +    % Apparently delayed_write can require two closes
    --- End diff --
    
    https://github.com/basho/lager/blob/master/src/lager_util.erl#L165-L167
    
    I'm assuming that if there's outstanding data to be written the first close becomes a flush of that buffer.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-log pull request #13: Make couch_log smarter

Posted by rnewson <gi...@git.apache.org>.
Github user rnewson commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-log/pull/13#discussion_r71570192
  
    --- Diff: src/couch_log_config.erl ---
    @@ -0,0 +1,107 @@
    +% 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.
    +%
    +% Based on Bob Ippolitto's mochiglobal.erl
    +
    +-module(couch_log_config).
    +
    +
    +-export([
    +    init/0,
    +    reconfigure/0,
    +    get/1
    +]).
    +
    +
    +-define(MOD_NAME, couch_log_config_dyn).
    +-define(ERL_FILE, "couch_log_config_dyn.erl").
    +
    +
    +-spec init() -> ok.
    +init() ->
    +    reconfigure().
    +
    +
    +-spec reconfigure() -> ok.
    +reconfigure() ->
    +    {ok, ?MOD_NAME, Bin} = compile:forms(forms(), [verbose, report_errors]),
    +    code:purge(?MOD_NAME),
    +    {module, ?MOD_NAME} = code:load_binary(?MOD_NAME, ?ERL_FILE, Bin),
    +    ok.
    +
    +
    +-spec get(atom()) -> term().
    +get(Key) ->
    +    ?MOD_NAME:get(Key).
    +
    +
    +-spec entries() -> [string()].
    +entries() ->
    +    [
    +        {level, "level", "info"},
    +        {level_int, "level", "info"},
    --- End diff --
    
    not `2`?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-log pull request #13: Make couch_log smarter

Posted by rnewson <gi...@git.apache.org>.
Github user rnewson commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-log/pull/13#discussion_r71571355
  
    --- Diff: src/couch_log_writer.erl ---
    @@ -0,0 +1,83 @@
    +% 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.
    +%
    +% @doc Moduels wishing to handle writing log
    --- End diff --
    
    modules


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-log pull request #13: Make couch_log smarter

Posted by rnewson <gi...@git.apache.org>.
Github user rnewson commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-log/pull/13#discussion_r71570483
  
    --- Diff: src/couch_log_config_listener.erl ---
    @@ -11,36 +11,46 @@
     % the License.
     
     -module(couch_log_config_listener).
    --vsn(2).
     -behaviour(config_listener).
     
    -% public interface
    --export([subscribe/0]).
    -
    -% config_listener callback
    --export([handle_config_change/5, handle_config_terminate/3]).
    -
    -subscribe() ->
    -    Settings = [
    -        {backend, config:get("log", "backend", "stderr")},
    -        {level, config:get("log", "level", "notice")}
    -    ],
    -    ok = config:listen_for_changes(?MODULE, Settings),
    -    ok.
    -
    -handle_config_change("log", "backend", Value, _, Settings) ->
    -    {level, Level} = lists:keyfind(level, 1, Settings),
    -    couch_log:set_level(Level),
    -    {ok, lists:keyreplace(backend, 1, Settings, {backend, Value})};
    -handle_config_change("log", "level", Value, _, Settings) ->
    -    couch_log:set_level(Value),
    -    {ok, lists:keyreplace(level, 1, Settings, {level, Value})};
    +
    +-export([
    +    start/0
    +]).
    +
    +-export([
    +    handle_config_change/5,
    +    handle_config_terminate/3
    +]).
    +
    +
    +start() ->
    +    ok = config:listen_for_changes(?MODULE, nil).
    +
    +
    +handle_config_change("log", Key, _, _, _) ->
    +    case Key of
    +        "level" ->
    +            couch_log_config:reconfigure();
    +        "max_term_size" ->
    +            couch_log_config:reconfigure();
    +        "max_message_size" ->
    +            couch_log_config:reconfigure();
    +        _ ->
    +            % Someone may have changed the config for
    +            % the writer so we need to re-initialize.
    +            couch_server:reconfigure()
    --- End diff --
    
    so why have the case statement at all?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-log pull request #13: Make couch_log smarter

Posted by davisp <gi...@git.apache.org>.
Github user davisp commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-log/pull/13#discussion_r71573580
  
    --- Diff: src/couch_log_util.erl ---
    @@ -0,0 +1,145 @@
    +% 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.
    +
    +-module(couch_log_util).
    +
    --- End diff --
    
    vsn is for gen_servers.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-log pull request #13: Make couch_log smarter

Posted by davisp <gi...@git.apache.org>.
Github user davisp commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-log/pull/13#discussion_r71573220
  
    --- Diff: src/couch_log_config.erl ---
    @@ -0,0 +1,107 @@
    +% 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.
    +%
    +% Based on Bob Ippolitto's mochiglobal.erl
    +
    +-module(couch_log_config).
    +
    +
    +-export([
    +    init/0,
    +    reconfigure/0,
    +    get/1
    +]).
    +
    +
    +-define(MOD_NAME, couch_log_config_dyn).
    +-define(ERL_FILE, "couch_log_config_dyn.erl").
    +
    +
    +-spec init() -> ok.
    +init() ->
    +    reconfigure().
    +
    +
    +-spec reconfigure() -> ok.
    +reconfigure() ->
    +    {ok, ?MOD_NAME, Bin} = compile:forms(forms(), [verbose, report_errors]),
    +    code:purge(?MOD_NAME),
    +    {module, ?MOD_NAME} = code:load_binary(?MOD_NAME, ?ERL_FILE, Bin),
    +    ok.
    +
    +
    +-spec get(atom()) -> term().
    +get(Key) ->
    +    ?MOD_NAME:get(Key).
    +
    +
    +-spec entries() -> [string()].
    +entries() ->
    +    [
    +        {level, "level", "info"},
    +        {level_int, "level", "info"},
    --- End diff --
    
    It gets converted below in `transform/1`. level_int mirrors the level config entry and just exists so we're not constantly re-converting that value for each log call.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-log pull request #13: Make couch_log smarter

Posted by rnewson <gi...@git.apache.org>.
Github user rnewson commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-log/pull/13#discussion_r71570388
  
    --- Diff: src/couch_log_config_listener.erl ---
    @@ -11,36 +11,46 @@
     % the License.
     
     -module(couch_log_config_listener).
    --vsn(2).
    --- End diff --
    
    :cry: 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-log pull request #13: Make couch_log smarter

Posted by davisp <gi...@git.apache.org>.
Github user davisp commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-log/pull/13#discussion_r71573300
  
    --- Diff: src/couch_log_config_listener.erl ---
    @@ -11,36 +11,46 @@
     % the License.
     
     -module(couch_log_config_listener).
    --vsn(2).
    --- End diff --
    
    Its not a gen_server and doesn't have a `code_change/3` function so it was stupid to have.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-log pull request #13: Make couch_log smarter

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/couchdb-couch-log/pull/13


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-log pull request #13: Make couch_log smarter

Posted by rnewson <gi...@git.apache.org>.
Github user rnewson commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-log/pull/13#discussion_r71571641
  
    --- Diff: src/couch_log_writer_file.erl ---
    @@ -0,0 +1,124 @@
    +% 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.
    +
    +-module(couch_log_writer_file).
    +-behaviour(couch_log_writer).
    +
    +
    +-export([
    +    init/0,
    +    terminate/2,
    +    write/2
    +]).
    +
    +
    +-include_lib("kernel/include/file.hrl").
    +-include("couch_log.hrl").
    +
    +
    +-record(st, {
    +    file_path,
    +    fd,
    +    inode,
    +    last_check
    +}).
    +
    +
    +-define(CHECK_INTERVAL, 30000000).
    +
    +
    +init() ->
    +    FilePath = config:get("log", "file", "./couch.log"),
    +    Opts = [append, raw] ++ buffer_opt(),
    +    case filelib:ensure_dir(FilePath) of
    +        ok ->
    +            case file:open(FilePath, Opts) of
    +                {ok, Fd} ->
    +                    case file:read_file_info(FilePath) of
    +                        {ok, FInfo} ->
    +                            {ok, #st{
    +                                file_path = FilePath,
    +                                fd = Fd,
    +                                inode = FInfo#file_info.inode,
    +                                last_check = os:timestamp()
    +                            }};
    +                        FInfoError ->
    +                            FInfoError
    +                    end;
    +                OpenError ->
    +                    OpenError
    +            end;
    +        EnsureDirError ->
    +            EnsureDirError
    +    end.
    +
    +
    +terminate(_, St) ->
    +    % Apparently delayed_write can require two closes
    --- End diff --
    
    reference?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-log pull request #13: Make couch_log smarter

Posted by rnewson <gi...@git.apache.org>.
Github user rnewson commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-log/pull/13#discussion_r71571255
  
    --- Diff: src/couch_log_util.erl ---
    @@ -0,0 +1,145 @@
    +% 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.
    +
    +-module(couch_log_util).
    +
    +
    +-export([
    +    should_log/1,
    +    iso8601_timestamp/0,
    +    get_msg_id/0,
    +
    +    level_to_integer/1,
    +    level_to_atom/1,
    +    level_to_string/1,
    +
    +    string_p/1
    +]).
    +
    +
    +-include("couch_log.hrl").
    +
    +
    +-spec should_log(#log_entry{} | atom()) -> boolean().
    +should_log(#log_entry{level = Level}) ->
    +    should_log(Level);
    +
    +should_log(Level) ->
    +    level_to_integer(Level) >= couch_log_config:get(level_int).
    +
    +
    +-spec iso8601_timestamp() -> string().
    +iso8601_timestamp() ->
    +    {_,_,Micro} = Now = os:timestamp(),
    +    {{Year,Month,Date},{Hour,Minute,Second}} = calendar:now_to_datetime(Now),
    +    Format = "~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~2.10.0B.~6.10.0BZ",
    +    io_lib:format(Format, [Year, Month, Date, Hour, Minute, Second, Micro]).
    +
    +
    +-spec get_msg_id() -> string().
    +get_msg_id() ->
    +    case erlang:get(nonce) of
    +        undefined -> "--------";
    +        MsgId -> MsgId
    +    end.
    +
    +
    +-spec level_to_integer(atom() | string() | integer()) -> integer().
    +level_to_integer(L) when L >= 0, L =< 9 -> L;
    +level_to_integer(debug)                 -> 1;
    +level_to_integer(info)                  -> 2;
    +level_to_integer(notice)                -> 3;
    +level_to_integer(warning)               -> 4;
    +level_to_integer(error)                 -> 5;
    +level_to_integer(critical)              -> 6;
    +level_to_integer(alert)                 -> 7;
    +level_to_integer(emergency)             -> 8;
    +level_to_integer(none)                  -> 9;
    +level_to_integer("debug")               -> 1;
    +level_to_integer("info")                -> 2;
    +level_to_integer("notice")              -> 3;
    +level_to_integer("warning")             -> 4;
    +level_to_integer("warn")                -> 4;
    +level_to_integer("error")               -> 5;
    +level_to_integer("err")                 -> 5;
    +level_to_integer("critical")            -> 6;
    +level_to_integer("crit")                -> 6;
    +level_to_integer("alert")               -> 7;
    +level_to_integer("emergency")           -> 8;
    +level_to_integer("emerg")               -> 8;
    +level_to_integer("none")                -> 9;
    +level_to_integer("1")                   -> 1;
    +level_to_integer("2")                   -> 2;
    +level_to_integer("3")                   -> 3;
    +level_to_integer("4")                   -> 4;
    +level_to_integer("5")                   -> 5;
    +level_to_integer("6")                   -> 6;
    +level_to_integer("7")                   -> 7;
    +level_to_integer("8")                   -> 8;
    +level_to_integer("9")                   -> 9.
    +
    +
    +-spec level_to_atom(atom() | string() | integer()) -> atom().
    +level_to_atom(L) when is_atom(L)    -> L;
    +level_to_atom("1")                  -> debug;
    +level_to_atom("debug")              -> debug;
    +level_to_atom("2")                  -> info;
    +level_to_atom("info")               -> info;
    +level_to_atom("3")                  -> notice;
    +level_to_atom("notice")             -> notice;
    +level_to_atom("4")                  -> warning;
    +level_to_atom("warning")            -> warning;
    +level_to_atom("warn")               -> warning;
    +level_to_atom("5")                  -> error;
    +level_to_atom("error")              -> error;
    +level_to_atom("err")                -> error;
    +level_to_atom("6")                  -> critical;
    +level_to_atom("critical")           -> critical;
    +level_to_atom("crit")               -> critical;
    +level_to_atom("7")                  -> alert;
    +level_to_atom("alert")              -> alert;
    +level_to_atom("8")                  -> emergency;
    +level_to_atom("emergency")          -> emergency;
    +level_to_atom("emerg")              -> emergency;
    +level_to_atom("9")                  -> none;
    +level_to_atom("none")               -> none;
    +level_to_atom(V) when is_integer(V) -> level_to_atom(integer_to_list(V));
    +level_to_atom(V) when is_list(V)    -> notice.
    --- End diff --
    
    defensive


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-log pull request #13: Make couch_log smarter

Posted by davisp <gi...@git.apache.org>.
Github user davisp commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-log/pull/13#discussion_r71573648
  
    --- Diff: src/couch_log_util.erl ---
    @@ -0,0 +1,145 @@
    +% 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.
    +
    +-module(couch_log_util).
    +
    +
    +-export([
    +    should_log/1,
    +    iso8601_timestamp/0,
    +    get_msg_id/0,
    +
    +    level_to_integer/1,
    +    level_to_atom/1,
    +    level_to_string/1,
    +
    +    string_p/1
    +]).
    +
    +
    +-include("couch_log.hrl").
    +
    +
    +-spec should_log(#log_entry{} | atom()) -> boolean().
    +should_log(#log_entry{level = Level}) ->
    +    should_log(Level);
    +
    +should_log(Level) ->
    +    level_to_integer(Level) >= couch_log_config:get(level_int).
    +
    +
    +-spec iso8601_timestamp() -> string().
    +iso8601_timestamp() ->
    +    {_,_,Micro} = Now = os:timestamp(),
    +    {{Year,Month,Date},{Hour,Minute,Second}} = calendar:now_to_datetime(Now),
    +    Format = "~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~2.10.0B.~6.10.0BZ",
    +    io_lib:format(Format, [Year, Month, Date, Hour, Minute, Second, Micro]).
    +
    +
    +-spec get_msg_id() -> string().
    +get_msg_id() ->
    +    case erlang:get(nonce) of
    +        undefined -> "--------";
    +        MsgId -> MsgId
    +    end.
    +
    +
    +-spec level_to_integer(atom() | string() | integer()) -> integer().
    +level_to_integer(L) when L >= 0, L =< 9 -> L;
    +level_to_integer(debug)                 -> 1;
    +level_to_integer(info)                  -> 2;
    +level_to_integer(notice)                -> 3;
    +level_to_integer(warning)               -> 4;
    +level_to_integer(error)                 -> 5;
    +level_to_integer(critical)              -> 6;
    +level_to_integer(alert)                 -> 7;
    +level_to_integer(emergency)             -> 8;
    +level_to_integer(none)                  -> 9;
    +level_to_integer("debug")               -> 1;
    +level_to_integer("info")                -> 2;
    +level_to_integer("notice")              -> 3;
    +level_to_integer("warning")             -> 4;
    +level_to_integer("warn")                -> 4;
    +level_to_integer("error")               -> 5;
    +level_to_integer("err")                 -> 5;
    +level_to_integer("critical")            -> 6;
    +level_to_integer("crit")                -> 6;
    +level_to_integer("alert")               -> 7;
    +level_to_integer("emergency")           -> 8;
    +level_to_integer("emerg")               -> 8;
    +level_to_integer("none")                -> 9;
    +level_to_integer("1")                   -> 1;
    +level_to_integer("2")                   -> 2;
    +level_to_integer("3")                   -> 3;
    +level_to_integer("4")                   -> 4;
    +level_to_integer("5")                   -> 5;
    +level_to_integer("6")                   -> 6;
    +level_to_integer("7")                   -> 7;
    +level_to_integer("8")                   -> 8;
    +level_to_integer("9")                   -> 9.
    +
    +
    +-spec level_to_atom(atom() | string() | integer()) -> atom().
    +level_to_atom(L) when is_atom(L)    -> L;
    +level_to_atom("1")                  -> debug;
    +level_to_atom("debug")              -> debug;
    +level_to_atom("2")                  -> info;
    +level_to_atom("info")               -> info;
    +level_to_atom("3")                  -> notice;
    +level_to_atom("notice")             -> notice;
    +level_to_atom("4")                  -> warning;
    +level_to_atom("warning")            -> warning;
    +level_to_atom("warn")               -> warning;
    +level_to_atom("5")                  -> error;
    +level_to_atom("error")              -> error;
    +level_to_atom("err")                -> error;
    +level_to_atom("6")                  -> critical;
    +level_to_atom("critical")           -> critical;
    +level_to_atom("crit")               -> critical;
    +level_to_atom("7")                  -> alert;
    +level_to_atom("alert")              -> alert;
    +level_to_atom("8")                  -> emergency;
    +level_to_atom("emergency")          -> emergency;
    +level_to_atom("emerg")              -> emergency;
    +level_to_atom("9")                  -> none;
    +level_to_atom("none")               -> none;
    +level_to_atom(V) when is_integer(V) -> level_to_atom(integer_to_list(V));
    +level_to_atom(V) when is_list(V)    -> notice.
    --- End diff --
    
    ?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-log pull request #13: Make couch_log smarter

Posted by davisp <gi...@git.apache.org>.
Github user davisp commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-log/pull/13#discussion_r71575682
  
    --- Diff: src/couch_log_monitor.erl ---
    @@ -0,0 +1,68 @@
    +% Copyright 2011 Cloudant
    --- End diff --
    
    Oops, and its not Cloudant.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-log issue #13: Make couch_log smarter

Posted by rnewson <gi...@git.apache.org>.
Github user rnewson commented on the issue:

    https://github.com/apache/couchdb-couch-log/pull/13
  
    I made some comments but I'm +1 on this once some/all of them are addressed. With the removal of lager, this is a nice simplification in an important area.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-log pull request #13: Make couch_log smarter

Posted by davisp <gi...@git.apache.org>.
Github user davisp commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-log/pull/13#discussion_r71573541
  
    --- Diff: src/couch_log_config_listener.erl ---
    @@ -11,36 +11,46 @@
     % the License.
     
     -module(couch_log_config_listener).
    --vsn(2).
     -behaviour(config_listener).
     
    -% public interface
    --export([subscribe/0]).
    -
    -% config_listener callback
    --export([handle_config_change/5, handle_config_terminate/3]).
    -
    -subscribe() ->
    -    Settings = [
    -        {backend, config:get("log", "backend", "stderr")},
    -        {level, config:get("log", "level", "notice")}
    -    ],
    -    ok = config:listen_for_changes(?MODULE, Settings),
    -    ok.
    -
    -handle_config_change("log", "backend", Value, _, Settings) ->
    -    {level, Level} = lists:keyfind(level, 1, Settings),
    -    couch_log:set_level(Level),
    -    {ok, lists:keyreplace(backend, 1, Settings, {backend, Value})};
    -handle_config_change("log", "level", Value, _, Settings) ->
    -    couch_log:set_level(Value),
    -    {ok, lists:keyreplace(level, 1, Settings, {level, Value})};
    +
    +-export([
    +    start/0
    +]).
    +
    +-export([
    +    handle_config_change/5,
    +    handle_config_terminate/3
    +]).
    +
    +
    +start() ->
    +    ok = config:listen_for_changes(?MODULE, nil).
    +
    +
    +handle_config_change("log", Key, _, _, _) ->
    +    case Key of
    +        "level" ->
    +            couch_log_config:reconfigure();
    +        "max_term_size" ->
    +            couch_log_config:reconfigure();
    +        "max_message_size" ->
    +            couch_log_config:reconfigure();
    +        _ ->
    +            % Someone may have changed the config for
    +            % the writer so we need to re-initialize.
    +            couch_server:reconfigure()
    --- End diff --
    
    Oops, typo, that's supposed to be couch_log_server:reconfigure. Notice that the other three are couch_log_config. The difference here is that level, max_term_size (which I can delete now) and max_message_size are options that are stored in the dynamic module to avoid constant ets table lookups. Everything else affects the writer which is in couch_log_server.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---