Class: Logify::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/logify/logger.rb

Constant Summary collapse

ANONYMOUS =
'(Anonymous)'
MAX_LENGTH =
32
NEWLINE =
"\n"
SEPARATOR =
' | '
MONITOR =
Monitor.new
NONE =
5
FATAL =
4
ERROR =
3
WARN =
2
INFO =
1
DEBUG =
0
DEFAULT =
WARN
LEVEL_MAP =
{
  none:  NONE,
  fatal: FATAL,
  error: ERROR,
  warn:  WARN,
  info:  INFO,
  debug: DEBUG,
}.freeze
PREFIX_FATAL =
'F: '
PREFIX_ERROR =
'E: '
PREFIX_WARN =
'W: '
PREFIX_INFO =
'I: '
PREFIX_DEBUG =
'D: '
PREFIX_LONG_FATAL =
'!!!! '
PREFIX_LONG_ERROR =
'>>>> '
PREFIX_LONG_WARN =
'**** '
PREFIX_LONG_INFO =
'===> '
PREFIX_LONG_DEBUG =
'     '

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Logger

Create a new logger object.

Parameters:

  • id (String, nil)

    the ID of the logger object to create



103
104
105
# File 'lib/logify/logger.rb', line 103

def initialize(id)
  @id = id
end

Class Method Details

.level(name) ⇒ Object



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
# File 'lib/logify/logger.rb', line 26

def level(name)
  constant = name.to_s.upcase

  class_eval <<-EOH, __FILE__, __LINE__ + 1
    def #{name}(message = nil, &block)
      if Logify.level <= #{constant}
        buffer = ''

        if Logify.level == #{DEBUG}
          buffer << formatted_id
          buffer << SEPARATOR
          buffer << PREFIX_LONG_#{constant}
        else
          buffer << PREFIX_#{constant}
        end

        buffer << message if message
        buffer << yield   if block_given?
        buffer << "#{NEWLINE}"

        MONITOR.synchronize { Logify.io.write(buffer) }

        true
      end
    end
  EOH
end

Instance Method Details

#debug(message = nil, &block) ⇒ true

Write a new debug message to the current IO object.

Examples:

Write a :debug message

log.debug 'This is a message'

Write a lazy evaluated :debug message

log.debug { perform_complex_operation }

Parameters:

  • message (String) (defaults to: nil)

    the message to log

  • block (Proc)

    the block to call that returns a string to write

Returns:

  • (true)


95
# File 'lib/logify/logger.rb', line 95

level :debug

#error(message = nil, &block) ⇒ true

Write a new error message to the current IO object.

Examples:

Write a :error message

log.error 'This is a message'

Write a lazy evaluated :error message

log.error { perform_complex_operation }

Parameters:

  • message (String) (defaults to: nil)

    the message to log

  • block (Proc)

    the block to call that returns a string to write

Returns:

  • (true)


92
# File 'lib/logify/logger.rb', line 92

level :error

#fatal(message = nil, &block) ⇒ true

Write a new fatal message to the current IO object.

Examples:

Write a :fatal message

log.fatal 'This is a message'

Write a lazy evaluated :fatal message

log.fatal { perform_complex_operation }

Parameters:

  • message (String) (defaults to: nil)

    the message to log

  • block (Proc)

    the block to call that returns a string to write

Returns:

  • (true)


91
# File 'lib/logify/logger.rb', line 91

level :fatal

#info(message = nil, &block) ⇒ true

Write a new info message to the current IO object.

Examples:

Write a :info message

log.info 'This is a message'

Write a lazy evaluated :info message

log.info { perform_complex_operation }

Parameters:

  • message (String) (defaults to: nil)

    the message to log

  • block (Proc)

    the block to call that returns a string to write

Returns:

  • (true)


94
# File 'lib/logify/logger.rb', line 94

level :info

#warn(message = nil, &block) ⇒ true

Write a new warn message to the current IO object.

Examples:

Write a :warn message

log.warn 'This is a message'

Write a lazy evaluated :warn message

log.warn { perform_complex_operation }

Parameters:

  • message (String) (defaults to: nil)

    the message to log

  • block (Proc)

    the block to call that returns a string to write

Returns:

  • (true)


93
# File 'lib/logify/logger.rb', line 93

level :warn