Class: SemanticLogger::Appender::Http
- Defined in:
- lib/semantic_logger/appender/http.rb
Overview
Log to any HTTP(S) server that accepts log messages in JSON form
Features:
-
JSON Formatted messages.
-
Uses a persistent http connection, if the server supports it.
-
SSL encryption (https).
Example:
appender = SemanticLogger::Appender::Http.new(
url: 'http://localhost:8088/path'
)
# Optional: Exclude health_check log entries, etc.
appender.filter = Proc.new { |log| log. !~ /(health_check|Not logged in)/}
SemanticLogger.add_appender(appender)
Direct Known Subclasses
Instance Attribute Summary collapse
-
#application ⇒ Object
Returns the value of attribute application.
-
#compress ⇒ Object
Returns the value of attribute compress.
-
#header ⇒ Object
Returns the value of attribute header.
-
#host ⇒ Object
Returns the value of attribute host.
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#ssl_options ⇒ Object
readonly
Returns the value of attribute ssl_options.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#username ⇒ Object
Returns the value of attribute username.
Attributes inherited from Base
Attributes inherited from Base
Instance Method Summary collapse
-
#default_formatter ⇒ Object
Use the JSON formatter.
-
#initialize(options, &block) ⇒ Http
constructor
Create HTTP(S) log appender.
-
#log(log) ⇒ Object
Forward log messages to HTTP Server.
-
#reopen ⇒ Object
Re-open after process fork.
Methods inherited from Base
colorized_formatter, #flush, json_formatter, #level
Methods inherited from Base
#benchmark, default_level, default_level=, #fast_tag, #level, #level=, #payload, #pop_tags, #push_tags, #silence, #tagged, #tags, #with_payload
Constructor Details
#initialize(options, &block) ⇒ Http
Create HTTP(S) log appender
Parameters:
url: [String]
Valid URL to post to.
Example: http://example.com/some_path
To enable SSL include https in the URL.
Example: https://example.com/some_path
verify_mode will default: OpenSSL::SSL::VERIFY_PEER
application: [String]
Name of this application to appear in log messages.
Default: SemanticLogger.application
host: [String]
Name of this host to appear in log messages.
Default: SemanticLogger.host
username: [String]
User name for basic Authentication.
Default: nil ( do not use basic auth )
password: [String]
Password for basic Authentication.
compress: [true|false]
Whether to compress the JSON string with GZip.
Default: false
ssl: [Hash]
Specific SSL options: For more details see NET::HTTP.start
ca_file, ca_path, cert, cert_store, ciphers, key, open_timeout, read_timeout, ssl_timeout,
ssl_version, use_ssl, verify_callback, verify_depth and verify_mode.
level: [:trace | :debug | :info | :warn | :error | :fatal]
Override the log level for this appender.
Default: SemanticLogger.default_level
filter: [Regexp|Proc]
RegExp: Only include log messages where the class name matches the supplied.
regular expression. All other messages will be ignored.
Proc: Only include log messages where the supplied Proc returns true
The Proc must return true or false.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/semantic_logger/appender/http.rb', line 69 def initialize(, &block) @options = .dup level = @options.delete(:level) filter = @options.delete(:filter) @url = .delete(:url) @ssl_options = .delete(:ssl) @username = .delete(:username) @password = .delete(:password) @application = .delete(:application) || 'Semantic Logger' @host = .delete(:host) || SemanticLogger.host @compress = .delete(:compress) || false raise(ArgumentError, "Unknown options: #{.inspect}") if .size > 0 raise(ArgumentError, 'Missing mandatory parameter :url') unless @url @header = { 'Accept' => 'application/json', 'Content-Type' => 'application/json' } @header['Content-Encoding'] = 'gzip' if @compress uri = URI.parse(@url) (@ssl_options ||= {})[:use_ssl] = true if uri.scheme == 'https' @server = uri.host raise(ArgumentError, "Invalid format for :url: #{@url.inspect}. Should be similar to: 'http://hostname:port/path'") unless @url @port = uri.port @username = uri.user if !@username && uri.user @password = uri.password if !@password && uri.password @path = uri.request_uri reopen # Pass on the level and custom formatter if supplied super(level, filter, &block) end |
Instance Attribute Details
#application ⇒ Object
Returns the value of attribute application.
23 24 25 |
# File 'lib/semantic_logger/appender/http.rb', line 23 def application @application end |
#compress ⇒ Object
Returns the value of attribute compress.
23 24 25 |
# File 'lib/semantic_logger/appender/http.rb', line 23 def compress @compress end |
#header ⇒ Object
Returns the value of attribute header.
23 24 25 |
# File 'lib/semantic_logger/appender/http.rb', line 23 def header @header end |
#host ⇒ Object
Returns the value of attribute host.
23 24 25 |
# File 'lib/semantic_logger/appender/http.rb', line 23 def host @host end |
#http ⇒ Object (readonly)
Returns the value of attribute http.
24 25 26 |
# File 'lib/semantic_logger/appender/http.rb', line 24 def http @http end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
24 25 26 |
# File 'lib/semantic_logger/appender/http.rb', line 24 def path @path end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
24 25 26 |
# File 'lib/semantic_logger/appender/http.rb', line 24 def port @port end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
24 25 26 |
# File 'lib/semantic_logger/appender/http.rb', line 24 def server @server end |
#ssl_options ⇒ Object (readonly)
Returns the value of attribute ssl_options.
24 25 26 |
# File 'lib/semantic_logger/appender/http.rb', line 24 def @ssl_options end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
24 25 26 |
# File 'lib/semantic_logger/appender/http.rb', line 24 def url @url end |
#username ⇒ Object
Returns the value of attribute username.
23 24 25 |
# File 'lib/semantic_logger/appender/http.rb', line 23 def username @username end |
Instance Method Details
#default_formatter ⇒ Object
Use the JSON formatter
122 123 124 |
# File 'lib/semantic_logger/appender/http.rb', line 122 def default_formatter self.class.json_formatter end |
#log(log) ⇒ Object
Forward log messages to HTTP Server
114 115 116 117 118 119 |
# File 'lib/semantic_logger/appender/http.rb', line 114 def log(log) return false if (level_index > (log.level_index || 0)) || !(log) # Filtered out? post(formatter.call(log, self)) end |
#reopen ⇒ Object
Re-open after process fork
108 109 110 111 |
# File 'lib/semantic_logger/appender/http.rb', line 108 def reopen # On Ruby v2.0 and greater, Net::HTTP.new uses a persistent connection if the server allows it @http = @ssl_options ? Net::HTTP.new(server, port, @ssl_options) : Net::HTTP.new(server, port) end |