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



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

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.



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

def token
  @token
end

#transportObject

Returns the value of attribute transport.



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

def transport
  @transport
end

Instance Method Details

#app_name(val) ⇒ Object

Sets default app_name for each message



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

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

#closeObject



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

def close
  transport.close
end

#closed?Boolean

Check if IO is closed

Returns:

  • (Boolean)


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

def closed?
  transport && transport.closed?
end

#facility(val) ⇒ Object

Sets default facility for each message



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

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

#hostname(val) ⇒ Object

Sets default hostname for each message



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

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)


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

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



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

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