Class: SyslogTls::Logger

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport, token = nil) ⇒ Logger

Logger accepts transport which should implement IO methods close, closed? and write



27
28
29
30
31
# File 'lib/syslog_tls/logger.rb', line 27

def initialize(transport, token=nil)
  @transport = transport
  @default_header = SyslogTls::Header.new
  @default_structured_data = SyslogTls::StructuredData.new(token)
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



22
23
24
# File 'lib/syslog_tls/logger.rb', line 22

def token
  @token
end

#transportObject

Returns the value of attribute transport.



23
24
25
# File 'lib/syslog_tls/logger.rb', line 23

def transport
  @transport
end

Instance Method Details

#app_name(val) ⇒ Object

Sets default app_name for each message



44
45
46
# File 'lib/syslog_tls/logger.rb', line 44

def app_name(val)
  @default_header.app_name = val
end

#closeObject



58
59
60
# File 'lib/syslog_tls/logger.rb', line 58

def close
  transport.close
end

#closed?Boolean

Check if IO is closed

Returns:

  • (Boolean)


54
55
56
# File 'lib/syslog_tls/logger.rb', line 54

def closed?
  transport && transport.closed?
end

#facility(val) ⇒ Object

Sets default facility for each message



34
35
36
# File 'lib/syslog_tls/logger.rb', line 34

def facility(val)
  @default_header.facility = val
end

#hostname(val) ⇒ Object

Sets default hostname for each message



39
40
41
# File 'lib/syslog_tls/logger.rb', line 39

def hostname(val)
  @default_header.hostname = val
end

#log(severity, message, time: nil) {|m.header| ... } ⇒ Object

Send log message with severity to syslog

Yields:

  • (m.header)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/syslog_tls/logger.rb', line 63

def log(severity, message, time: nil)
  time ||= Time.now

  m = SyslogTls::Message.new

  # Include authentication header
  m.structured_data << @default_structured_data

  # Adjust header with current timestamp and severity
  m.header = @default_header.dup
  m.header.severity = severity
  m.header.timestamp = time

  yield m.header if block_given?

  m.msg = message

  transport.write(m.to_s)
end

#procid(val) ⇒ Object

Sets default procid for message



49
50
51
# File 'lib/syslog_tls/logger.rb', line 49

def procid(val)
  @default_header.procid = val
end