Kouhei Sutou
null+****@clear*****
Sun Jan 5 23:35:12 JST 2014
Kouhei Sutou 2014-01-05 23:35:12 +0900 (Sun, 05 Jan 2014) New Revision: 129873eabc51f13aa8d37a1e0517f1be4bd33f1c https://github.com/ranguba/chupa-text/commit/129873eabc51f13aa8d37a1e0517f1be4bd33f1c Message: Add DefaultLogger Added files: lib/chupa-text/default-logger.rb test/test-default-logger.rb Modified files: lib/chupa-text.rb Modified: lib/chupa-text.rb (+1 -0) =================================================================== --- lib/chupa-text.rb 2014-01-05 23:16:20 +0900 (dbd15d0) +++ lib/chupa-text.rb 2014-01-05 23:35:12 +0900 (ba2cd5e) @@ -19,6 +19,7 @@ require "chupa-text/version" require "chupa-text/error" require "chupa-text/size-parser" +require "chupa-text/default-logger" require "chupa-text/configuration" require "chupa-text/configuration-loader" require "chupa-text/decomposer" Added: lib/chupa-text/default-logger.rb (+123 -0) 100644 =================================================================== --- /dev/null +++ lib/chupa-text/default-logger.rb 2014-01-05 23:35:12 +0900 (968ea4e) @@ -0,0 +1,123 @@ +# Copyright (C) 2013 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +require "logger" + +module ChupaText + # The default logger for ChupaText. Logger options are retrieved from + # environment variables. + # + # Here are environment variables to be used: + # + # ## `CHUPA_TEXT_LOG_OUTPUT` + # + # It specifies log output. + # + # * `-`: ChupaText outputs to the standard output. It is the default. + # * `+`: ChupaText outputs to the standard error output. + # * Others: It is used as file name. ChupaText outputs to the file. + # + # ## `CHUPA_TEXT_LOG_ROTATION_PERIOD` + # + # It specifies log rotation period. + # + # It is ignored when `CHUPA_TEXT_LOG_OUTPUT` is `-` (the standard + # output) or `+` (the standard error output). + # + # * `daily`: Log file is rotated daily. + # * `weekly`: Log file is rotated weekly. + # * `monthly`: Log file is rotated monthly. + # * Others: Invalid value. It is ignored. + # + # ## `CHUPA_TEXT_LOG_N_GENERATIONS` + # + # It specifies how many old log files are kept. + # + # It is ignored when (a) `CHUPA_TEXT_LOG_OUTPUT` is `-` (the + # standard output) or `+` (the standard error output) or (b) + # `CHUPA_TEXT_LOG_RATATION_PERIOD` is valid value. + # + # The default value is `7`. + # + # ## `CHUPA_TEXT_LOG_LEVEL` + # + # It specifies log verbosity. + # + # The default value is `info`. + # + # * `unknown`: ChupaText outputs only unknown messages. + # * `fatal`: ChupaText outputs `unknown` level messages and + # unhandleable error messages. + # * `error`: ChupaText outputs `fatal` level messages and + # handleable error messages. + # * `warn`: ChupaText outputs `error` level messages and warning + # level messages. + # * `info`: ChupaText outputs `warn` level messages and generic useful + # information messages. + # * `debug`: ChupaText outputs all messages. + class DefaultLogger < Logger + def initialize + super(output_device, default_shift_age, default_shift_size) + self.level = default_level + end + + private + def output_device + output = ENV["CHUPA_TEXT_LOG_OUTPUT"] || "-" + case output + when "-" + STDOUT + when "+" + STDERR + else + output + end + end + + def default_shift_age + rotation_period = ENV["CHUPA_TEXT_LOG_ROTATION_PERIOD"] + case rotation_period + when "daily", "weekly", "monthly" + return rotation_period + end + + n_generations = ENV["CHUPA_TEXT_LOG_N_GENERATIONS"] || "7" + begin + Integer(n_generations) + rescue ArgumentError + nil + end + end + + def default_shift_size + max_size = ENV["CHUPA_TEXT_LOG_MAX_SIZE"] || "1MB" + begin + SizeParser.parse(max_size) + rescue SizeParser::InvalidSizeError + nil + end + end + + def default_level + level_name = (ENV["CHUPA_TEXT_LOG_LEVEL"] || "info").upcase + if Logger::Severity.const_defined?(level_name) + Logger::Severity.const_get(level_name) + else + Logger::Severity::INFO + end + end + end +end Added: test/test-default-logger.rb (+181 -0) 100644 =================================================================== --- /dev/null +++ test/test-default-logger.rb 2014-01-05 23:35:12 +0900 (a5aeeeb) @@ -0,0 +1,181 @@ +# Copyright (C) 2013 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +class TestDefaultLogger < Test::Unit::TestCase + def setup + @env = ENV.to_h + end + + def teardown + ENV.replace(@env) + end + + private + def default_n_generations + 7 + end + + def default_max_size + 1024 * 1024 + end + + sub_test_case("output") do + def output(value) + ENV["CHUPA_TEXT_LOG_OUTPUT"] = value + logger = ChupaText::DefaultLogger.new + device = logger.instance_variable_get(:@logdev) + device.dev + end + + def test_minus + assert_equal(STDOUT, output("-")) + end + + def test_plus + assert_equal(STDERR, output("+")) + end + + def test_string + file = Tempfile.new("chupa-text-default-logger-output") + assert_equal(file.path, output(file.path).path) + end + + def test_default + assert_equal(STDOUT, output(nil)) + end + end + + sub_test_case("rotation period") do + def rotation_period(value) + file = Tempfile.new("chupa-text-default-logger-output") + ENV["CHUPA_TEXT_LOG_OUTPUT"] = file.path + ENV["CHUPA_TEXT_LOG_ROTATION_PERIOD"] = value + logger = ChupaText::DefaultLogger.new + device = logger.instance_variable_get(:@logdev) + device.instance_variable_get(:@shift_age) + end + + def test_daily + assert_equal("daily", rotation_period("daily")) + end + + def test_weekly + assert_equal("weekly", rotation_period("weekly")) + end + + def test_monthly + assert_equal("monthly", rotation_period("monthly")) + end + + def test_default + assert_equal(default_n_generations, rotation_period(nil)) + end + + def test_invalid + assert_equal(default_n_generations, rotation_period(nil)) + end + end + + sub_test_case("N generation") do + def n_generations(value) + file = Tempfile.new("chupa-text-default-logger-output") + ENV["CHUPA_TEXT_LOG_OUTPUT"] = file.path + ENV["CHUPA_TEXT_LOG_N_GENERATIONS"] = value + logger = ChupaText::DefaultLogger.new + device = logger.instance_variable_get(:@logdev) + device.instance_variable_get(:@shift_age) + end + + def test_integer + assert_equal(29, n_generations("29")) + end + + def test_default + assert_equal(default_n_generations, n_generations(nil)) + end + + def test_invalid + assert_equal(default_n_generations, n_generations("2.9")) + end + end + + sub_test_case("max size") do + def max_size(value) + file = Tempfile.new("chupa-text-default-logger-output") + ENV["CHUPA_TEXT_LOG_OUTPUT"] = file.path + ENV["CHUPA_TEXT_LOG_MAX_SIZE"] = value + logger = ChupaText::DefaultLogger.new + device = logger.instance_variable_get(:@logdev) + device.instance_variable_get(:@shift_size) + end + + def test_unit + assert_equal(1024, max_size("1KB")) + end + + def test_value_only + assert_equal(1024, max_size("1024")) + end + + def test_default + assert_equal(default_max_size, max_size(nil)) + end + + def test_invalid + assert_equal(default_max_size, max_size("max-size")) + end + end + + sub_test_case("level") do + def level(value) + ENV["CHUPA_TEXT_LOG_LEVEL"] = value + logger = ChupaText::DefaultLogger.new + logger.level + end + + def test_debug + assert_equal(Logger::DEBUG, level("debug")) + end + + def test_info + assert_equal(Logger::INFO, level("info")) + end + + def test_warn + assert_equal(Logger::WARN, level("warn")) + end + + def test_error + assert_equal(Logger::ERROR, level("error")) + end + + def test_fatal + assert_equal(Logger::FATAL, level("fatal")) + end + + def test_unknown + assert_equal(Logger::UNKNOWN, level("unknown")) + end + + def test_default + assert_equal(Logger::INFO, level(nil)) + end + + def test_invalid + assert_equal(Logger::INFO, level("invalid")) + end + end +end -------------- next part -------------- HTML����������������������������...Download