• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# GRPC contains the General RPC module.
16module GRPC
17  def self.logger=(logger_obj)
18    # Need a free variable here to keep value of logger_obj for logger closure
19    @logger = logger_obj
20
21    extend(
22      Module.new do
23        def logger
24          @logger
25        end
26      end
27    )
28  end
29
30  # DefaultLogger is a module included in GRPC if no other logging is set up for
31  # it.  See ../spec/spec_helpers an example of where other logging is added.
32  module DefaultLogger
33    def logger
34      LOGGER
35    end
36
37    private
38
39    # NoopLogger implements the methods of Ruby's conventional logging interface
40    # that are actually used internally within gRPC with a noop implementation.
41    class NoopLogger
42      def info(_ignored)
43      end
44
45      def debug(_ignored)
46      end
47
48      def warn(_ignored)
49      end
50    end
51
52    LOGGER = NoopLogger.new
53  end
54
55  # Inject the noop #logger if no module-level logger method has been injected.
56  extend DefaultLogger unless methods.include?(:logger)
57end
58