-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[rb] Initial implementation of Logger class
- Loading branch information
Showing
19 changed files
with
174 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# encoding: utf-8 | ||
# | ||
# 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, :debug, :debug?, | ||
:info, :info?, | ||
:warn, :warn?, | ||
:error, :error?, | ||
:fatal, :fatal?, | ||
:level, :level= | ||
|
||
def initialize | ||
@logger = ::Logger.new($stdout) | ||
@logger.progname = 'Selenium' | ||
@logger.level = ($DEBUG ? :debug : :warn) | ||
@logger.formatter = proc do |severity, time, progname, msg| | ||
"#{time.strftime('%F %T')} #{severity} #{progname} #{msg}\n" | ||
end | ||
end | ||
|
||
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 | ||
if debug? | ||
@logger.instance_variable_get(:@logdev).instance_variable_get(:@dev) | ||
else | ||
File.new(Platform.null_device, 'w') | ||
end | ||
end | ||
end # Logger | ||
end # WebDriver | ||
end # Selenium |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# encoding: utf-8 | ||
# | ||
# 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 File.expand_path('../../spec_helper', __FILE__) | ||
|
||
module Selenium | ||
module WebDriver | ||
describe Logger do | ||
around do |example| | ||
WebDriver.instance_variable_set(:@logger, nil) # reset cache | ||
debug = $DEBUG | ||
$DEBUG = false | ||
example.call | ||
$DEBUG = debug | ||
end | ||
|
||
it 'logs warnings by default' do | ||
expect(WebDriver.logger.level).to eq(2) | ||
end | ||
|
||
it 'logs everything if $DEBUG is set to true' do | ||
$DEBUG = true | ||
expect(WebDriver.logger.level).to eq(0) | ||
end | ||
|
||
it 'allows to change level during execution' do | ||
WebDriver.logger.level = :info | ||
expect(WebDriver.logger.level).to eq(1) | ||
end | ||
|
||
it 'outputs to stdout by default' do | ||
expect { WebDriver.logger.warn('message') }.to output(/WARN Selenium message/).to_stdout | ||
end | ||
|
||
it 'allows to output to file' do | ||
begin | ||
WebDriver.logger.output = 'test.log' | ||
WebDriver.logger.warn('message') | ||
expect(File.read('test.log')).to include('WARN Selenium message') | ||
ensure | ||
File.delete('test.log') | ||
end | ||
end | ||
end | ||
end # WebDriver | ||
end # Selenium |