File: logger.rb

package info (click to toggle)
ruby-selenium-webdriver 4.38.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,400 kB
  • sloc: ruby: 7,721; javascript: 125; makefile: 6
file content (215 lines) | stat: -rw-r--r-- 6,400 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# frozen_string_literal: true

# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The SFC licenses this file
# to you 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.

require 'forwardable'
require 'logger'

module Selenium
  module WebDriver
    #
    # @example Enable full logging
    #   Selenium::WebDriver.logger.level = :debug
    #
    # @example Log to file
    #   Selenium::WebDriver.logger.output = 'selenium.log'
    #
    # @example Use logger manually
    #   Selenium::WebDriver.logger.info('This is info message')
    #   Selenium::WebDriver.logger.warn('This is warning message')
    #
    class Logger
      extend Forwardable

      def_delegators :@logger,
                     :close,
                     :debug?,
                     :info?,
                     :warn?,
                     :error?,
                     :fatal, :fatal?,
                     :level

      #
      # @param [String] progname Allow child projects to use Selenium's Logger pattern
      #
      def initialize(progname = 'Selenium', default_level: nil, ignored: nil, allowed: nil)
        default_level ||= $DEBUG || ENV.key?('DEBUG') ? :debug : :warn

        @logger = create_logger(progname, level: default_level)
        @ignored = Array(ignored)
        @allowed = Array(allowed)
        @first_warning = false
      end

      def level=(level)
        if level == :info && @logger.level == :info
          info(':info is now the default log level, to see additional logging, set log level to :debug')
        end

        @logger.level = level
      end

      #
      # Changes logger output to a new IO.
      #
      # @param [String] io
      #
      def output=(io)
        @logger.reopen(io)
      end

      #
      # Returns IO object used by logger internally.
      #
      # Normally, we would have never needed it, but we want to
      # use it as IO object for all child processes to ensure their
      # output is redirected there.
      #
      # It is only used in debug level, in other cases output is suppressed.
      #
      # @api private
      #
      def io
        @logger.instance_variable_get(:@logdev).dev
      end

      #
      # Will not log the provided ID.
      #
      # @param [Array, Symbol] ids
      #
      def ignore(*ids)
        @ignored += Array(ids).flatten
      end

      #
      # Will only log the provided ID.
      #
      # @param [Array, Symbol] ids
      #
      def allow(*ids)
        @allowed += Array(ids).flatten
      end

      #
      # Used to supply information of interest for debugging a problem
      # Overrides default #debug to skip ignored messages by provided id
      #
      # @param [String] message
      # @param [Symbol, Array<Symbol>] id
      # @yield see #deprecate
      #
      def debug(message, id: [], &block)
        discard_or_log(:debug, message, id, &block)
      end

      #
      # Used to supply information of general interest
      #
      # @param [String] message
      # @param [Symbol, Array<Symbol>] id
      # @yield see #deprecate
      #
      def info(message, id: [], &block)
        discard_or_log(:info, message, id, &block)
      end

      #
      # Used to supply information that suggests an error occurred
      #
      # @param [String] message
      # @param [Symbol, Array<Symbol>] id
      # @yield see #deprecate
      #
      def error(message, id: [], &block)
        discard_or_log(:error, message, id, &block)
      end

      #
      # Used to supply information that suggests action be taken by user
      #
      # @param [String] message
      # @param [Symbol, Array<Symbol>] id
      # @yield see #deprecate
      #
      def warn(message, id: [], &block)
        discard_or_log(:warn, message, id, &block)
      end

      #
      # Marks code as deprecated with/without replacement.
      #
      # @param [String] old
      # @param [String, nil] new
      # @param [Symbol, Array<Symbol>] id
      # @param [String] reference
      # @yield appends additional message to end of provided template
      #
      def deprecate(old, new = nil, id: [], reference: '', &block)
        id = Array(id)
        return if @ignored.include?(:deprecations)

        id << :deprecations if @allowed.include?(:deprecations)

        message = "[DEPRECATION] #{old} is deprecated"
        message << if new
                     ". Use #{new} instead."
                   else
                     ' and will be removed in a future release.'
                   end
        message << " See explanation for this deprecation: #{reference}." unless reference.empty?

        discard_or_log(:warn, message, id, &block)
      end

      private

      def create_logger(name, level:)
        logger = ::Logger.new($stderr)
        logger.progname = name
        logger.level = level
        logger.formatter = proc do |severity, time, progname, msg|
          "#{time.strftime('%F %T')} #{severity} #{progname} #{msg}\n".force_encoding('UTF-8')
        end

        logger
      end

      def discard_or_log(level, message, id)
        id = Array(id)
        return if @ignored.intersect?(id)
        return if @allowed.any? && !@allowed.intersect?(id)

        return if ::Logger::Severity.const_get(level.upcase) < @logger.level

        unless @first_warning
          @first_warning = true
          info("Details on how to use and modify Selenium logger:\n", id: [:logger_info]) do
            "https://selenium.dev/documentation/webdriver/troubleshooting/logging\n"
          end
        end

        msg = id.empty? ? message : "[#{id.map(&:inspect).join(', ')}] #{message} "
        msg += " #{yield}" if block_given?

        @logger.send(level) { msg }
      end
    end # Logger
  end # WebDriver
end # Selenium