Class: Logger

Inherits:
Object show all
Includes:
Severity
Defined in:
opal/stdlib/logger.rb

Defined Under Namespace

Modules: Severity Classes: Formatter

Constant Summary

SEVERITY_LABELS =
Severity.constants.map { |s| [(Severity.const_get s), s.to_s] }.to_h

Constants included from Severity

Severity::DEBUG, Severity::ERROR, Severity::FATAL, Severity::INFO, Severity::UNKNOWN, Severity::WARN

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pipe) ⇒ Logger

Returns a new instance of Logger


38
39
40
41
42
# File 'opal/stdlib/logger.rb', line 38

def initialize(pipe)
  @pipe = pipe
  @level = DEBUG
  @formatter = Formatter.new
end

Instance Attribute Details

#formatterObject

Returns the value of attribute formatter


36
37
38
# File 'opal/stdlib/logger.rb', line 36

def formatter
  @formatter
end

#levelObject

Returns the value of attribute level


34
35
36
# File 'opal/stdlib/logger.rb', line 34

def level
  @level
end

#prognameObject

Returns the value of attribute progname


35
36
37
# File 'opal/stdlib/logger.rb', line 35

def progname
  @progname
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object


98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'opal/stdlib/logger.rb', line 98

def add(severity, message = nil, progname = nil, &block)
  return true if (severity ||= UNKNOWN) < @level
  progname ||= @progname
  unless message
    if block_given?
      message = yield
    else
      message = progname
      progname = @progname
    end
  end
  @pipe.write(@formatter.call(SEVERITY_LABELS[severity] || 'ANY', ::Time.now, progname, message))
  true
end

#debug(progname = nil, &block) ⇒ Object


58
59
60
# File 'opal/stdlib/logger.rb', line 58

def debug(progname = nil, &block)
  add DEBUG, nil, progname, &block
end

#debug?Boolean

Returns:


82
83
84
# File 'opal/stdlib/logger.rb', line 82

def debug?
  @level <= DEBUG
end

#error(progname = nil, &block) ⇒ Object


66
67
68
# File 'opal/stdlib/logger.rb', line 66

def error(progname = nil, &block)
  add ERROR, nil, progname, &block
end

#error?Boolean

Returns:


90
91
92
# File 'opal/stdlib/logger.rb', line 90

def error?
  @level <= ERROR
end

#fatal(progname = nil, &block) ⇒ Object


70
71
72
# File 'opal/stdlib/logger.rb', line 70

def fatal(progname = nil, &block)
  add FATAL, nil, progname, &block
end

#fatal?Boolean

Returns:


94
95
96
# File 'opal/stdlib/logger.rb', line 94

def fatal?
  @level <= FATAL
end

#info(progname = nil, &block) ⇒ Object


54
55
56
# File 'opal/stdlib/logger.rb', line 54

def info(progname = nil, &block)
  add INFO, nil, progname, &block
end

#info?Boolean

Returns:


78
79
80
# File 'opal/stdlib/logger.rb', line 78

def info?
  @level <= INFO
end

#unknown(progname = nil, &block) ⇒ Object


74
75
76
# File 'opal/stdlib/logger.rb', line 74

def unknown(progname = nil, &block)
  add UNKNOWN, nil, progname, &block
end

#warn(progname = nil, &block) ⇒ Object


62
63
64
# File 'opal/stdlib/logger.rb', line 62

def warn(progname = nil, &block)
  add WARN, nil, progname, &block
end

#warn?Boolean

Returns:


86
87
88
# File 'opal/stdlib/logger.rb', line 86

def warn?
  @level <= WARN
end