Project

General

Profile

Feature #9860 ยป logger-application.patch

hsbt (Hiroshi SHIBATA), 05/24/2014 09:20 AM

View differences:

lib/logger.rb
741 741
  class LogDevice
742 742
    include Period
743 743
  end
744

  
745

  
746
  #
747
  # == Description
748
  #
749
  # Logger::Application --- Add logging support to your application.
750
  #
751
  # == Usage
752
  #
753
  # 1. Define your application class as a sub-class of this class.
754
  # 2. Override the +run+ method in your class to do many things.
755
  # 3. Instantiate it and invoke #start.
756
  #
757
  # == Example
758
  #
759
  #   class FooApp < Logger::Application
760
  #     def initialize(foo_app, application_specific, arguments)
761
  #       super('FooApp') # Name of the application.
762
  #     end
763
  #
764
  #     def run
765
  #       ...
766
  #       log(WARN, 'warning', 'my_method1')
767
  #       ...
768
  #       @log.error('my_method2') { 'Error!' }
769
  #       ...
770
  #     end
771
  #   end
772
  #
773
  #   status = FooApp.new(....).start
774
  #
775
  class Application
776
    include Logger::Severity
777

  
778
    # Name of the application given at initialize.
779
    attr_reader :appname
780

  
781
    #
782
    # :call-seq:
783
    #   Logger::Application.new(appname = '')
784
    #
785
    # == Args
786
    #
787
    # +appname+:: Name of the application.
788
    #
789
    # == Description
790
    #
791
    # Create an instance.  Log device is +STDERR+ by default.  This can be
792
    # changed with #set_log.
793
    #
794
    def initialize(appname = nil)
795
      @appname = appname
796
      @log = Logger.new(STDERR)
797
      @log.progname = @appname
798
      @level = @log.level
799
    end
800

  
801
    #
802
    # Start the application.  Return the status code.
803
    #
804
    def start
805
      status = -1
806
      begin
807
        log(INFO, "Start of #{ @appname }.")
808
        status = run
809
      rescue
810
        log(FATAL, "Detected an exception. Stopping ... #{$!} (#{$!.class})\n" << $@.join("\n"))
811
      ensure
812
        log(INFO, "End of #{ @appname }. (status: #{ status.to_s })")
813
      end
814
      status
815
    end
816

  
817
    # Logger for this application.  See the class Logger for an explanation.
818
    def logger
819
      @log
820
    end
821

  
822
    #
823
    # Sets the logger for this application.  See the class Logger for an
824
    # explanation.
825
    #
826
    def logger=(logger)
827
      @log = logger
828
      @log.progname = @appname
829
      @log.level = @level
830
    end
831

  
832
    #
833
    # Sets the log device for this application.  See <tt>Logger.new</tt> for
834
    # an explanation of the arguments.
835
    #
836
    def set_log(logdev, shift_age = 0, shift_size = 1024000)
837
      @log = Logger.new(logdev, shift_age, shift_size)
838
      @log.progname = @appname
839
      @log.level = @level
840
    end
841

  
842
    def log=(logdev)
843
      set_log(logdev)
844
    end
845

  
846
    #
847
    # Set the logging threshold, just like <tt>Logger#level=</tt>.
848
    #
849
    def level=(level)
850
      @level = level
851
      @log.level = @level
852
    end
853

  
854
    #
855
    # See Logger#add.  This application's +appname+ is used.
856
    #
857
    def log(severity, message = nil, &block)
858
      @log.add(severity, message, @appname, &block) if @log
859
    end
860

  
861
  private
862

  
863
    def run
864
      # TODO: should be an NotImplementedError
865
      raise RuntimeError.new('Method run must be defined in the derived class.')
866
    end
867
  end
868 744
end
lib/logger/application.rb
1
#
2
# == Description
3
#
4
# Logger::Application --- Add logging support to your application.
5
#
6
# == Usage
7
#
8
# 1. Define your application class as a sub-class of this class.
9
# 2. Override the +run+ method in your class to do many things.
10
# 3. Instantiate it and invoke #start.
11
#
12
# == Example
13
#
14
#   class FooApp < Logger::Application
15
#     def initialize(foo_app, application_specific, arguments)
16
#       super('FooApp') # Name of the application.
17
#     end
18
#
19
#     def run
20
#       ...
21
#       log(WARN, 'warning', 'my_method1')
22
#       ...
23
#       @log.error('my_method2') { 'Error!' }
24
#       ...
25
#     end
26
#   end
27
#
28
#   status = FooApp.new(....).start
29
#
30

  
31
require 'logger'
32

  
33
class Logger
34
  class Application
35
    include Logger::Severity
36

  
37
    # Name of the application given at initialize.
38
    attr_reader :appname
39

  
40
    #
41
    # :call-seq:
42
    #   Logger::Application.new(appname = '')
43
    #
44
    # == Args
45
    #
46
    # +appname+:: Name of the application.
47
    #
48
    # == Description
49
    #
50
    # Create an instance.  Log device is +STDERR+ by default.  This can be
51
    # changed with #set_log.
52
    #
53
    def initialize(appname = nil)
54
      @appname = appname
55
      @log = Logger.new(STDERR)
56
      @log.progname = @appname
57
      @level = @log.level
58
    end
59

  
60
    #
61
    # Start the application.  Return the status code.
62
    #
63
    def start
64
      status = -1
65
      begin
66
        log(INFO, "Start of #{ @appname }.")
67
        status = run
68
      rescue
69
        log(FATAL, "Detected an exception. Stopping ... #{$!} (#{$!.class})\n" << $@.join("\n"))
70
      ensure
71
        log(INFO, "End of #{ @appname }. (status: #{ status.to_s })")
72
      end
73
      status
74
    end
75

  
76
    # Logger for this application.  See the class Logger for an explanation.
77
    def logger
78
      @log
79
    end
80

  
81
    #
82
    # Sets the logger for this application.  See the class Logger for an
83
    # explanation.
84
    #
85
    def logger=(logger)
86
      @log = logger
87
      @log.progname = @appname
88
      @log.level = @level
89
    end
90

  
91
    #
92
    # Sets the log device for this application.  See <tt>Logger.new</tt> for
93
    # an explanation of the arguments.
94
    #
95
    def set_log(logdev, shift_age = 0, shift_size = 1024000)
96
      @log = Logger.new(logdev, shift_age, shift_size)
97
      @log.progname = @appname
98
      @log.level = @level
99
    end
100

  
101
    def log=(logdev)
102
      set_log(logdev)
103
    end
104

  
105
    #
106
    # Set the logging threshold, just like <tt>Logger#level=</tt>.
107
    #
108
    def level=(level)
109
      @level = level
110
      @log.level = @level
111
    end
112

  
113
    #
114
    # See Logger#add.  This application's +appname+ is used.
115
    #
116
    def log(severity, message = nil, &block)
117
      @log.add(severity, message, @appname, &block) if @log
118
    end
119

  
120
  private
121

  
122
    def run
123
      # TODO: should be an NotImplementedError
124
      raise RuntimeError.new('Method run must be defined in the derived class.')
125
    end
126
  end
127
end
test/logger/test_application.rb
1 1
# coding: US-ASCII
2 2
require 'test/unit'
3
require 'logger'
3
require 'logger/application'
4 4
require 'tempfile'
5 5

  
6 6
class TestLoggerApplication < Test::Unit::TestCase