├── .github └── repository_metadata.yml ├── .gitignore ├── .snyk ├── .travis.yml ├── Gemfile ├── LE.gemspec ├── LICENSE ├── README.md ├── Rakefile ├── cortex.yaml ├── lib ├── le.rb └── le │ ├── host.rb │ └── host │ └── http.rb └── test ├── fixtures └── log │ └── .gitignore ├── host_spec.rb ├── http_spec.rb ├── le_spec.rb └── spec_helper.rb /.github/repository_metadata.yml: -------------------------------------------------------------------------------- 1 | 2 | repo_owners: 3 | - dublin-splinter 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | .rvmrc 19 | .ruby-* 20 | *.log 21 | -------------------------------------------------------------------------------- /.snyk: -------------------------------------------------------------------------------- 1 | version: v1.25.0 2 | ignore: {} 3 | patch: {} 4 | exclude: {} 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 2.3.1 4 | - 2.0.0 5 | - 1.9.3 6 | - jruby-19mode 7 | before_install: 8 | - gem install bundler 9 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec -------------------------------------------------------------------------------- /LE.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'le' 5 | 6 | Gem::Specification.new do |gem| 7 | gem.name = "le" 8 | gem.version = "2.7.6" 9 | gem.date = Time.now 10 | gem.summary = "Logentries plugin" 11 | gem.licenses = ["MIT"] 12 | gem.description =< and rubygems 14 | . 15 | 16 | 17 | Example 18 | ------- 19 | 20 | Rails.logger.info("information message") 21 | Rails.logger.warn("warning message") 22 | Rails.logger.debug("debug message") 23 | 24 | 25 | Howto 26 | ----- 27 | 28 | You must first register your account details with Logentries. 29 | 30 | Once you have logged in to Logentries, create a new host with a name of your choice. 31 | Inside this host, create a new logfile, selecting `Token TCP` (or `Plain TCP/UDP` if using UDP) 32 | as the source type. 33 | 34 | Heroku 35 | ------ 36 | 37 | To install the gem you must edit the Gemfile in your local heroku environment 38 | 39 | Add the following line: 40 | 41 | gem 'le' 42 | 43 | Then from the cmd line run the following command: 44 | 45 | bundle install 46 | 47 | This will install the gem on your local environment. 48 | 49 | The next step is to configure the default rails logger to use the logentries 50 | logger. 51 | 52 | 53 | In your environment configuration file ( for production : `config/environments/production.rb`), add the following: 54 | 55 | Rails.logger = Le.new('LOGENTRIES_TOKEN') 56 | 57 | If you want to keep logging locally in addition to sending logs to logentries, just add local parameter after the key. 58 | By default, this will write to the standard Rails log or to STDOUT if not using Rails: 59 | 60 | Rails.logger = Le.new('LOGENTRIES_TOKEN', :local => true) 61 | 62 | You may specify the local log device by providing a filename (String) or IO object (typically STDOUT, STDERR, or an open file): 63 | 64 | Rails.logger = Le.new('LOGENTRIES_TOKEN', :local => 'log/my_custom_log.log') 65 | 66 | If you want the gem to use SSL when streaming logs to Logentries, add the ssl parameter and set it to true: 67 | 68 | Rails.logger = Le.new('LOGENTRIES_TOKEN', :ssl => true) 69 | 70 | If you want to print debug messages for the gem to a file called logentriesGem.log, add this: 71 | 72 | Rails.logger = Le.new('LOGENTRIES_TOKEN', :debug => true) 73 | 74 | If you want to use ActiveSupport::TaggedLogging logging, add this: 75 | 76 | Rails.logger = Le.new('LOGENTRIES_TOKEN', :tag => true) 77 | 78 | You can also specify the default level of the logger by adding a : 79 | 80 | Rails.logger = Le.new('LOGENTRIES_TOKEN', :log_level => Logger::) 81 | 82 | For the `LOGENTRIES_TOKEN` argument, paste the token for the logfile you created earlier in the Logentries UI or empty string for 83 | a UDP connection. Additionally, when connecting via UDP, be sure to specify a port using the udp_port parameter: 84 | 85 | Rails.logger = Le.new('', :udp_port => 13287) 86 | 87 | Users have the option of using `data.logentries.com` which uses ports 80 and 443 for insecure and secure connections respectively. 88 | 89 | Rails.logger = Le.new('', :data_endpoint => true) 90 | 91 | 92 | 93 | Step for setting up DataHub 94 | --------------------------- 95 | 96 | **datahub_endpoint - User Defined Array** 97 | 98 | datahub_endpoint = Array ["127.0.0.1", "10000"] 99 | datahub_endpoint is a user defined variable array for a datahub_endpoint 100 | The 1st parameter is a String which is the DataHub Instance's IP Address. Entering ANY value in this field will disable your Token-based 101 | logging, set your Token to "" and will direct all log events to your specified DataHub IP Address. 102 | 103 | The 2nd parameter is a String which is the DataHub Port value, default is 10000 but this can be changed on your DataHub Machine. 104 | This port number must be set, on your DataHub Machine's leproxy settings your /etc/leproxy/leproxyLocal.config file. It's default is 10000 105 | NOTE: if datahub_endpoint has been assigned an IP address and SSL = true, your server will fail gracefully. 106 | When using Datahub do not enable SSL = true 107 | 108 | 109 | **host_id** 110 | 111 | host_id = "abc1234" 112 | Enter_host_id inside the quotation marks. Leaving this empty leave the host_id empty and thus not appear in your log events. 113 | 114 | 115 | **custom_host_name - User Defined Array** 116 | 117 | custom_host = Array[ true, "mikes_app_server"] 118 | The 1st parameter is a Boolean value to use the custom host name. 119 | The 2nd parameter is a String which is the custom_host_name you'd like to assign. 120 | 121 | If the 2nd parameter is left as in custom_host = Array[ true, ""] the code will attempt to get your host machine's name using the socket.gethostname method. 122 | 123 | 124 | 125 | Using the above user defined variable settings, you can now also specify the several of the optional arguments for the logger constructor by adding: 126 | 127 | Rails.logger = Le.new(token, :ssl=>ssl, :datahub_endpoint=>datahub_endpoint, :host_id=>host_id, :custom_host=>custom_host) 128 | 129 | 130 | Now, simply use `Rails.logger.info("message")` inside your code to send logs to Logentries 131 | 132 | 133 | Contact Support 134 | ------ 135 | 136 | Please email our support team at support@logentries.com if you need any help. 137 | 138 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require 'rake/testtask' 3 | 4 | Rake::TestTask.new do |t| 5 | t.libs = ['lib','test'] 6 | t.test_files = Dir.glob("test/**/*_spec.rb").sort 7 | t.verbose = true 8 | end 9 | 10 | task :default => [:test] 11 | task :spec => [:test] 12 | 13 | desc "Open an irb session preloaded with this library" 14 | task :console do 15 | sh "irb -rubygems -I lib -r le.rb" 16 | end 17 | -------------------------------------------------------------------------------- /cortex.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | info: 3 | title: Le Ruby 4 | description: Ruby logging support for logentries.com 5 | x-cortex-git: 6 | github: 7 | alias: r7org 8 | repository: rapid7/le_ruby 9 | x-cortex-tag: le-ruby 10 | x-cortex-type: service 11 | x-cortex-domain-parents: 12 | - tag: logsearch 13 | openapi: 3.0.1 14 | servers: 15 | - url: "/" 16 | -------------------------------------------------------------------------------- /lib/le.rb: -------------------------------------------------------------------------------- 1 | require File.join(File.dirname(__FILE__), 'le', 'host') 2 | 3 | require 'logger' 4 | 5 | module Le 6 | 7 | def self.new(token, options={}) 8 | 9 | opt_local = options[:local] || false 10 | opt_debug = options[:debug] || false 11 | opt_ssl = !options.include?(:ssl) ? true : options[:ssl] 12 | opt_tag = options[:tag] || false 13 | opt_log_level = options[:log_level] || Logger::DEBUG 14 | 15 | opt_datahub_enabled = options[:datahub_enabled] || false 16 | opt_datahub_endpoint = options[:datahub_endpoint] || ['', 10000] 17 | opt_datahub_ip = options[:datahub_ip] || '' 18 | opt_datahub_port = options[:datahub_port] || 10000 19 | opt_host_id = options[:host_id] || '' 20 | opt_host_name_enabled = options[:host_name_enabled] || false 21 | opt_host_name = options[:host_name] || '' 22 | opt_custom_host = options[:custom_host] || [false, ''] 23 | 24 | opt_udp_port = options[:udp_port] || nil 25 | opt_use_data_endpoint = options[:data_endpoint] || false 26 | 27 | self.checkParams(token, opt_datahub_enabled, opt_udp_port) 28 | 29 | 30 | host = Le::Host.new(token, opt_local, opt_debug, opt_ssl, opt_datahub_endpoint, opt_host_id, opt_custom_host, opt_udp_port, opt_use_data_endpoint) 31 | 32 | if defined?(ActiveSupport::TaggedLogging) && opt_tag 33 | logger = ActiveSupport::TaggedLogging.new(Logger.new(host)) 34 | elsif defined?(ActiveSupport::Logger) 35 | logger = ActiveSupport::Logger.new(host) 36 | logger.formatter = host.formatter if host.respond_to?(:formatter) 37 | else 38 | logger = Logger.new(host) 39 | logger.formatter = host.formatter if host.respond_to?(:formatter) 40 | end 41 | 42 | logger.level = opt_log_level 43 | 44 | logger 45 | end 46 | 47 | def self.checkParams(token, opt_datahub_enabled, opt_udp_port) 48 | # Check if the key is valid UUID format 49 | 50 | if (!opt_datahub_enabled && !opt_udp_port) # test Token only when DataHub and UDP are not enabled 51 | if (token =~ /\A(urn:uuid:)?[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}\z/i) == nil 52 | puts "\nLE: It appears the LOGENTRIES_TOKEN you entered is invalid!\n" 53 | else 54 | (token="") 55 | end 56 | end 57 | end 58 | 59 | end 60 | -------------------------------------------------------------------------------- /lib/le/host.rb: -------------------------------------------------------------------------------- 1 | module Le 2 | module Host 3 | 4 | def self.new(token, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp_port, use_data_endpoint) 5 | 6 | Le::Host::HTTP.new(token, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp_port, use_data_endpoint) 7 | end 8 | 9 | module InstanceMethods 10 | def formatter 11 | proc do |severity, datetime, _, msg| 12 | message = "#{datetime} " 13 | message << format_message(msg, severity) 14 | end 15 | end 16 | 17 | def format_message(message_in, severity) 18 | message_in = message_in.inspect unless message_in.is_a?(String) 19 | 20 | "severity=#{severity}, #{message_in.lstrip}" 21 | end 22 | end 23 | 24 | end 25 | end 26 | 27 | require File.join(File.dirname(__FILE__), 'host', 'http') 28 | -------------------------------------------------------------------------------- /lib/le/host/http.rb: -------------------------------------------------------------------------------- 1 | require 'socket' 2 | require 'openssl' 3 | require 'thread' 4 | require 'timeout' 5 | require 'uri' 6 | 7 | module Le 8 | module Host 9 | class HTTP 10 | API_SERVER = 'data.logentries.com' 11 | DATA_ENDPOINT = 'data.logentries.com' 12 | DATA_PORT_UNSECURE = 80 13 | DATA_PORT_SECURE = 443 14 | API_PORT = 10000 15 | API_SSL_PORT = 20000 16 | SHUTDOWN_COMMAND = "DIE!DIE!" # magic command string for async worker to shutdown 17 | SHUTDOWN_MAX_WAIT = 10 # max seconds to wait for queue to clear on shutdown 18 | SHUTDOWN_WAIT_STEP = 0.2 # sleep duration (seconds) while waiting to shutdown 19 | 20 | 21 | include Le::Host::InstanceMethods 22 | #! attr_accessor :token, :queue, :started, :thread, :conn, :local, :debug, :ssl, :datahub_enabled, :dathub_ip, :datahub_port, :host_id, :custom_host, :host_name_enabled, :host_name 23 | attr_accessor :token, :queue, :started, :thread, :conn, :local, :debug, :ssl, :datahub_enabled, :datahub_ip, :datahub_port, :datahub_endpoint, :host_id, :host_name_enabled, :host_name, :custom_host, :udp_port, :use_data_endpoint 24 | 25 | 26 | def initialize(token, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp_port, use_data_endpoint) 27 | if local 28 | device = if local.class <= TrueClass 29 | if defined?(Rails) 30 | Rails.root.join("log","#{Rails.env}.log") 31 | else 32 | STDOUT 33 | end 34 | else 35 | local 36 | end 37 | @logger_console = Logger.new(device) 38 | end 39 | 40 | @local = !!local 41 | @debug= debug 42 | @ssl = ssl 43 | @udp_port = udp_port 44 | @use_data_endpoint = use_data_endpoint 45 | 46 | @datahub_endpoint = datahub_endpoint 47 | if !@datahub_endpoint[0].empty? 48 | @datahub_enabled=true 49 | @datahub_ip="#{@datahub_endpoint[0]}" 50 | @datahub_port=@datahub_endpoint[1] 51 | else 52 | @datahub_enabled=false 53 | end 54 | 55 | 56 | if (@datahub_enabled && @ssl) 57 | puts ("\n\nYou Cannot have DataHub and SSL enabled at the same time. Please set SSL value to false in your environment.rb file or used Token-Based logging by leaving the Datahub IP address blank. Exiting application. \n\n") 58 | exit 59 | end 60 | 61 | 62 | #check if DataHub is enabled... if datahub is not enabled, set the token to the token's parameter. If DH is enabled, make the token empty. 63 | if (!datahub_enabled) 64 | @token = token 65 | else 66 | @token = '' 67 | 68 | #! NOTE THIS @datahub_port conditional MAY NEED TO BE CHANGED IF SSL CAN'T WORK WITH DH 69 | @datahub_port = @datahub_port.empty? ? API_SSL_PORT : datahub_port 70 | @datahub_ip = datahub_ip 71 | end 72 | 73 | @host_name_enabled=custom_host[0]; 74 | @host_name= custom_host[1]; 75 | 76 | 77 | # Check if host_id is empty -- if not assign, if so, make it an empty string. 78 | if !host_id.empty? 79 | @host_id = host_id 80 | @host_id = "host_id=#{host_id}" 81 | else 82 | @host_id='' 83 | end 84 | 85 | 86 | 87 | #assign host_name, if no host name is given and host_name_enabled = true... assign a host_name based on the machine name. 88 | if @host_name_enabled 89 | if host_name.empty? 90 | @host_name=Socket.gethostname 91 | end 92 | 93 | @host_name="host_name=#{@host_name}" 94 | end 95 | 96 | 97 | @queue = Queue.new 98 | @started = false 99 | @thread = nil 100 | 101 | if @debug 102 | self.init_debug 103 | end 104 | at_exit { shutdown! } 105 | end 106 | 107 | def init_debug 108 | filePath = "logentriesGem.log" 109 | if File.exist?('log/') 110 | filePath = "log/logentriesGem.log" 111 | end 112 | @debug_logger = Logger.new(filePath) 113 | end 114 | 115 | def dbg(message) 116 | if @debug 117 | @debug_logger.add(Logger::Severity::DEBUG, message) 118 | end 119 | end 120 | 121 | def write(message) 122 | 123 | if !host_id.empty? 124 | message = "#{message} #{ host_id }" 125 | end 126 | 127 | if host_name_enabled 128 | message="#{message} #{ host_name }" 129 | end 130 | 131 | if @local 132 | @logger_console.add(Logger::Severity::UNKNOWN, message) 133 | end 134 | 135 | if message.scan(/\n/).empty? 136 | @queue << "#{ @token } #{ message } \n" 137 | else 138 | @queue << "#{ message.gsub(/^/, "#{ @token } [#{ random_message_id }]") }\n" 139 | end 140 | 141 | 142 | if @started 143 | check_async_thread 144 | else 145 | start_async_thread 146 | end 147 | end 148 | 149 | def start_async_thread 150 | @thread = Thread.new { run() } 151 | dbg "LE: Asynchronous socket writer started" 152 | @started = true 153 | end 154 | 155 | def check_async_thread 156 | if not(@thread && @thread.alive?) 157 | @thread = Thread.new { run() } 158 | end 159 | end 160 | 161 | def close 162 | dbg "LE: Closing asynchronous socket writer" 163 | @started = false 164 | end 165 | 166 | def openConnection 167 | dbg "LE: Reopening connection to Logentries API server" 168 | 169 | if @use_data_endpoint 170 | host = DATA_ENDPOINT 171 | if @ssl 172 | port = DATA_PORT_SECURE 173 | else 174 | port = DATA_PORT_UNSECURE 175 | end 176 | else 177 | if @udp_port 178 | host = API_SERVER 179 | port = @udp_port 180 | elsif @datahub_enabled 181 | host = @datahub_ip 182 | port = @datahub_port 183 | else 184 | host = API_SERVER 185 | port = @ssl ? API_SSL_PORT: API_PORT 186 | end 187 | end 188 | 189 | if @udp_port 190 | @conn = UDPSocket.new 191 | @conn.connect(host, port) 192 | else 193 | socket = TCPSocket.new(host, port) 194 | 195 | if @ssl 196 | cert_store = OpenSSL::X509::Store.new 197 | cert_store.set_default_paths 198 | 199 | ssl_context = OpenSSL::SSL::SSLContext.new() 200 | ssl_context.cert_store = cert_store 201 | 202 | ssl_version_candidates = [:TLSv1_2, :TLSv1_1, :TLSv1] 203 | ssl_version_candidates = ssl_version_candidates.select { |version| OpenSSL::SSL::SSLContext::METHODS.include? version } 204 | if ssl_version_candidates.empty? 205 | raise "Could not find suitable TLS version" 206 | end 207 | # currently we only set the version when we have no choice 208 | ssl_context.ssl_version = ssl_version_candidates[0] if ssl_version_candidates.length == 1 209 | ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER 210 | ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context) 211 | ssl_socket.hostname = host if ssl_socket.respond_to?(:hostname=) 212 | ssl_socket.sync_close = true 213 | Timeout::timeout(10) do 214 | ssl_socket.connect 215 | end 216 | @conn = ssl_socket 217 | else 218 | @conn = socket 219 | end 220 | end 221 | 222 | dbg "LE: Connection established" 223 | end 224 | 225 | def reopenConnection 226 | closeConnection 227 | root_delay = 0.1 228 | loop do 229 | begin 230 | openConnection 231 | break 232 | rescue Timeout::Error, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError 233 | dbg "LE: Unable to connect to Logentries due to timeout(#{ $! })" 234 | rescue 235 | dbg "LE: Got exception in reopenConnection - #{ $! }" 236 | raise 237 | end 238 | root_delay *= 2 239 | if root_delay >= 10 240 | root_delay = 10 241 | end 242 | wait_for = (root_delay + rand(root_delay)).to_i 243 | dbg "LE: Waiting for #{ wait_for }ms" 244 | sleep(wait_for) 245 | end 246 | end 247 | 248 | def closeConnection 249 | begin 250 | if @conn.respond_to?(:sysclose) 251 | @conn.sysclose 252 | elsif @conn.respond_to?(:close) 253 | @conn.close 254 | end 255 | rescue 256 | dbg "LE: couldn't close connection, close with exception - #{ $! }" 257 | ensure 258 | @conn = nil 259 | end 260 | end 261 | 262 | def run 263 | reopenConnection 264 | 265 | loop do 266 | data = @queue.pop 267 | break if data == SHUTDOWN_COMMAND 268 | loop do 269 | begin 270 | @conn.write(data) 271 | rescue Timeout::Error, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError 272 | dbg "LE: Connection timeout(#{ $! }), try to reopen connection" 273 | reopenConnection 274 | next 275 | rescue 276 | dbg("LE: Got exception in run loop - #{ $! }") 277 | raise 278 | end 279 | 280 | break 281 | end 282 | end 283 | 284 | dbg "LE: Closing Asynchronous socket writer" 285 | 286 | closeConnection 287 | end 288 | 289 | private 290 | def random_message_id 291 | @random_message_id_sample_space ||= ('0'..'9').to_a.concat(('A'..'Z').to_a) 292 | (0..5).map{ @random_message_id_sample_space.sample }.join 293 | end 294 | 295 | # at_exit handler. 296 | # Attempts to clear the queue and terminate the async worker cleanly before process ends. 297 | def shutdown! 298 | return unless @started 299 | dbg "LE: commencing shutdown, queue has #{queue.size} entries to clear" 300 | queue << SHUTDOWN_COMMAND 301 | SHUTDOWN_MAX_WAIT.div(SHUTDOWN_WAIT_STEP).times do 302 | break if queue.empty? 303 | sleep SHUTDOWN_WAIT_STEP 304 | end 305 | dbg "LE: shutdown complete, queue is #{queue.empty? ? '' : 'not '}empty with #{queue.size} entries" 306 | end 307 | 308 | end 309 | end 310 | end 311 | -------------------------------------------------------------------------------- /test/fixtures/log/.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | -------------------------------------------------------------------------------- /test/host_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Le::Host do 4 | 5 | let(:token) { '11111111-2222-3333-aaaa-bbbbbbbbbbbb' } 6 | let(:local) { false } 7 | let(:debug) { false } 8 | let(:ssl) { true } 9 | let(:udp) { nil } 10 | 11 | 12 | let(:datahub_endpoint) { ["", 10000] } 13 | let(:host_id) { ""} 14 | let(:custom_host) { [false, ""]} 15 | let(:data_endpoint) {true} 16 | 17 | #let(:host) { Le::Host.new(token, local, debug, ssl) } 18 | let(:host) { Le::Host::HTTP.new(token, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp, data_endpoint) } 19 | specify { host.must_be_instance_of Le::Host::HTTP } 20 | 21 | end 22 | -------------------------------------------------------------------------------- /test/http_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | require 'pathname' 3 | 4 | describe Le::Host::HTTP do 5 | 6 | let(:token) { '11111111-2222-3333-aaaa-bbbbbbbbbbbb' } 7 | let(:local) { false } 8 | let(:debug) { false } 9 | let(:ssl) { false } 10 | let(:udp) { nil } 11 | 12 | let(:datahub_endpoint) { ["", 10000]} 13 | let(:host_id) {""} 14 | let(:custom_host) {[false, ""]} 15 | let(:endpoint) {false} 16 | 17 | 18 | 19 | # let(:host) { Le::Host::HTTP.new(token, local, debug, ssl) } 20 | let(:host) { Le::Host::HTTP.new(token, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp, endpoint) } 21 | 22 | let(:logger_console) { host.instance_variable_get(:@logger_console) } 23 | let(:logger_console_dev) { logger_console.instance_variable_get(:@logdev).dev } 24 | 25 | specify { host.must_be_instance_of Le::Host::HTTP } 26 | specify { host.local.must_equal false } 27 | specify { host.debug.must_equal false } 28 | specify { host.ssl.must_equal false } 29 | specify { host.udp_port.must_equal nil } 30 | specify {host_id.must_equal ""} 31 | specify {custom_host.must_equal [false, ""]} 32 | 33 | end 34 | -------------------------------------------------------------------------------- /test/le_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Le do 4 | 5 | let(:token) { '11111111-2222-3333-aaaa-bbbbbbbbbbbb' } 6 | let(:local) { false } 7 | let(:logger) { Le.new(token, local: local) } 8 | let(:logdev) { logger.instance_variable_get(:@logdev).dev } 9 | let(:logger_console) { logdev.instance_variable_get(:@logger_console) } 10 | let(:logger_console_dev) { logger_console.instance_variable_get(:@logdev).dev } 11 | 12 | 13 | describe "when non-Rails environment" do 14 | 15 | describe "when initialised with just a token" do 16 | let(:logger) { Le.new(token) } 17 | specify { logger.must_be_instance_of Logger } 18 | specify { logdev.must_be_instance_of Le::Host::HTTP } 19 | specify { logdev.local.must_equal false } 20 | specify { logger_console.must_be_nil } 21 | end 22 | 23 | describe "and :local is false" do 24 | specify { logdev.local.must_equal false } 25 | specify { logger_console.must_be_nil } 26 | end 27 | 28 | describe "and :local is true" do 29 | let(:local) { true } 30 | 31 | specify { logdev.local.must_equal true } 32 | specify { logger_console.must_be_instance_of Logger } 33 | specify { logger_console_dev.must_be_instance_of IO } 34 | end 35 | 36 | 37 | describe "and :local => " do 38 | let(:local_test_log) { Pathname.new(File.dirname(__FILE__)).join('fixtures','log','local_log.log') } 39 | let(:local) { log_file } 40 | 41 | describe "Pathname" do 42 | let(:log_file) { local_test_log } 43 | 44 | specify { logdev.must_be_instance_of Le::Host::HTTP } 45 | specify { logdev.local.must_equal true } 46 | specify { logger_console.must_be_instance_of Logger } 47 | specify { logger_console_dev.must_be_instance_of File } 48 | specify { logger_console_dev.path.must_match 'local_log.log' } 49 | end 50 | 51 | describe "path string" do 52 | let(:log_file) { local_test_log.to_s } 53 | 54 | specify { logdev.must_be_instance_of Le::Host::HTTP } 55 | specify { logdev.local.must_equal true } 56 | specify { logger_console.must_be_instance_of Logger } 57 | specify { logger_console_dev.must_be_instance_of File } 58 | specify { logger_console_dev.path.must_match 'local_log.log' } 59 | end 60 | 61 | describe "File" do 62 | let(:log_file) { File.new(local_test_log, 'w') } 63 | 64 | specify { logdev.must_be_instance_of Le::Host::HTTP } 65 | specify { logdev.local.must_equal true } 66 | specify { logger_console.must_be_instance_of Logger } 67 | specify { logger_console_dev.must_be_instance_of File } 68 | specify { logger_console_dev.path.must_match 'local_log.log' } 69 | end 70 | 71 | end 72 | 73 | end 74 | 75 | describe "when Rails environment" do 76 | before do 77 | class Rails 78 | def self.root 79 | Pathname.new(File.dirname(__FILE__)).join('fixtures') 80 | end 81 | def self.env 82 | 'test' 83 | end 84 | end 85 | end 86 | after do 87 | Object.send(:remove_const, :Rails) 88 | end 89 | 90 | describe "and :local is false" do 91 | specify { logdev.local.must_equal false } 92 | specify { logger_console.must_be_nil } 93 | end 94 | 95 | describe "and :local is true" do 96 | let(:local) { true } 97 | 98 | specify { logdev.local.must_equal true } 99 | specify { logger_console.must_be_instance_of Logger } 100 | specify { logger_console_dev.must_be_instance_of File } 101 | specify { logger_console_dev.path.must_match 'test.log' } 102 | end 103 | 104 | describe "and :local => " do 105 | let(:local_test_log) { Pathname.new(File.dirname(__FILE__)).join('fixtures','log','local_log.log') } 106 | let(:local) { log_file } 107 | 108 | describe "Pathname" do 109 | let(:log_file) { local_test_log } 110 | 111 | specify { logdev.must_be_instance_of Le::Host::HTTP } 112 | specify { logdev.local.must_equal true } 113 | specify { logger_console.must_be_instance_of Logger } 114 | specify { logger_console_dev.must_be_instance_of File } 115 | specify { logger_console_dev.path.must_match 'local_log.log' } 116 | end 117 | 118 | describe "path string" do 119 | let(:log_file) { local_test_log.to_s } 120 | 121 | specify { logdev.must_be_instance_of Le::Host::HTTP } 122 | specify { logdev.local.must_equal true } 123 | specify { logger_console.must_be_instance_of Logger } 124 | specify { logger_console_dev.must_be_instance_of File } 125 | specify { logger_console_dev.path.must_match 'local_log.log' } 126 | end 127 | 128 | describe "File" do 129 | let(:log_file) { File.new(local_test_log, 'w') } 130 | 131 | specify { logdev.must_be_instance_of Le::Host::HTTP } 132 | specify { logdev.local.must_equal true } 133 | specify { logger_console.must_be_instance_of Logger } 134 | specify { logger_console_dev.must_be_instance_of File } 135 | specify { logger_console_dev.path.must_match 'local_log.log' } 136 | end 137 | 138 | end 139 | 140 | end 141 | 142 | end 143 | -------------------------------------------------------------------------------- /test/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'minitest/autorun' 2 | require 'le' --------------------------------------------------------------------------------