Class: Coralogix::CoralogixLogger

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

Constant Summary collapse

false
@@stack_frame =
5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ CoralogixLogger

Constructor.



19
20
21
22
23
# File 'lib/coralogix_logger.rb', line 19

def initialize name
    @category = name
    @level=0
    @use_source_file = true
end

Instance Attribute Details

#use_source_fileObject

Returns the value of attribute use_source_file.



10
11
12
# File 'lib/coralogix_logger.rb', line 10

def use_source_file
  @use_source_file
end

Class Method Details

.configure(private_key, app_name, sub_system) ⇒ boolean

Configure coralogix logger with customer specific values



95
96
97
98
99
100
# File 'lib/coralogix_logger.rb', line 95

def self.configure private_key, app_name, sub_system
    private_key = (private_key.nil? || private_key.to_s.strip.empty?) ? FAILED_PRIVATE_KEY : private_key
    app_name = (app_name.nil? || app_name.to_s.strip.empty?) ? NO_APP_NAME : app_name
    sub_system = (sub_system.nil? || sub_system.to_s.strip.empty?) ? NO_SUB_SYSTEM : sub_system
    LoggerManager.configure(:privateKey => private_key, :applicationName => app_name, :subsystemName => sub_system) unless LoggerManager.configured
end

.debug_mode=(value) ⇒ Object

A setter for debug_mode. Default value is false. When set to true the coralogix logger will print output messages to a console and a file.



47
48
49
# File 'lib/coralogix_logger.rb', line 47

def self.debug_mode=(value)
    DebugLogger.debug_mode=value
end

.debug_mode?boolean

A getter for debug_mode. Default value is false. When set to true the coralogix logger will print output messages to a console and a file.



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

def self.debug_mode?
    DebugLogger.debug_mode
end

.disable_proxy=(value) ⇒ Object

A setter for disable_proxy. By default HTTP object will use proxy environment variable if exists. In some cases this migh be an issue When set to false the HTTP object will ignore any proxy.



75
76
77
# File 'lib/coralogix_logger.rb', line 75

def self.disable_proxy=(value)
    CoralogixHTTPSender.disable_proxy=value
end

.get_logger(name) ⇒ CoralogixLogger

A class method (static) to return a new instance of the current class. This is the most common pattern when using logging.



84
85
86
87
# File 'lib/coralogix_logger.rb', line 84

def self.get_logger name
    #Return a new instance of the current class.
    CoralogixLogger.send(:new, name)
end

A setter for print stack trace. Default value is false. When set to true the coralogix logger will print stack trace for each log line.



56
57
58
# File 'lib/coralogix_logger.rb', line 56

def self.print_stack_trace=(value)
    @@print_stack_trace=value
end

.reconnectObject

Spawn a new worker thread



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

def self.reconnect
    LoggerManager.reconnect
end

.stack_frame=(value) ⇒ Object

A setter for stack frame. Default value is 5. The stack frame to extract from the stack trace



65
66
67
# File 'lib/coralogix_logger.rb', line 65

def self.stack_frame=(value)
    @@stack_frame=value
end

Instance Method Details

#<<(x) ⇒ Object

Logger interface: Dump given message to the log device without any formatting.



180
181
182
# File 'lib/coralogix_logger.rb', line 180

def << x
    self.add 0, nil, x
end

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

Logger interface: Send log message if the given severity is high enough.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/coralogix_logger.rb', line 143

def add(severity, message = nil, progname = nil, &block)
    
    thread = ""

    begin
        severity ||= DEBUG
        if severity < @level
            return true
        end
        progname ||= @category
        if message.nil?
            if block_given?
                message = yield
            else
                message = progname
                progname = @category
            end
        end
        className = get_source_file if @use_source_file
        thread = Thread.current.object_id.to_s
    rescue Exception => e  
        DebugLogger.error e.message  
        DebugLogger.error e.backtrace.inspect
    end

    # Map Ruby Severity to Coralogix severity:
    #          Ruby Coralogix
    #    DEBUG  0      1
    #    INFO   1      3
    #    WARN   2      4
    #    ERROR  3      5
    #    FATAL  4      6
    LoggerManager.add_logline message, severity == 0 ? Severity::DEBUG : severity + 2, progname, :className => className, :threadId => thread
end

#closeObject

Logger interface: Close coralogix logger. Not implemented



198
199
200
# File 'lib/coralogix_logger.rb', line 198

def close
    # Not implemented
end

#flushObject

Flush all messages in buffer and send them immediately on the current thread.



109
110
111
# File 'lib/coralogix_logger.rb', line 109

def flush
    LoggerManager.flush
end

#formatter=(formatter) ⇒ Object

Logger interface: Set logger formatter



192
193
194
# File 'lib/coralogix_logger.rb', line 192

def formatter= formatter
    # Nothing to do here as we always use Coralogix format
end

#get_source_fileObject

Return the file name where the call to the logger was made. This will be used as the class name



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/coralogix_logger.rb', line 203

def get_source_file
    begin
        #        0                          1                               2                   3
        #logger.info(Rails logger) -> Logger.broadcast(Rails Logger)-> add(This class) -> get_source_file(This method)
        if  DebugLogger.debug_mode? && @@print_stack_trace
            DebugLogger.info "Stack trace:"
            DebugLogger.info caller_locations(0..10).join("\n")
        end
        
        file_location_path = caller_locations(@@stack_frame..@@stack_frame)[0].path
        File.basename(file_location_path, File.extname(file_location_path))
    rescue Exception => e  
        DebugLogger.error e.message  
        DebugLogger.error e.backtrace.inspect
        return nil
    end
end

#level=(level) ⇒ Object

Logger interface:

Set Logger level



29
30
31
# File 'lib/coralogix_logger.rb', line 29

def level= level
    @level=level
end

#log(severity, message, category: @category, className: "", methodName: "", threadId: Thread.current.object_id.to_s) ⇒ Object

Log a message.



122
123
124
# File 'lib/coralogix_logger.rb', line 122

def log severity, message, category: @category, className: "", methodName: "", threadId: Thread.current.object_id.to_s
    LoggerManager.add_logline message, severity, category, :className => className, :methodName => methodName, :threadId => threadId
end

#progname=(name) ⇒ Object

Logger interface: Set logger program name. This will be used as the category.



186
187
188
# File 'lib/coralogix_logger.rb', line 186

def progname= name
    @category = name
end