Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
85cf794
working on the scheduler
lvilya Aug 16, 2019
5f4be72
savingBranch
lvilya Aug 22, 2019
4590d4e
the final test is insihed
lvilya Aug 22, 2019
ef2c346
add bin folder to gitignore
lvilya Aug 22, 2019
e3b90ca
remive all bin folder changes"
lvilya Aug 22, 2019
e430073
add tests
lvilya Aug 22, 2019
5232804
remove spec folder
lvilya Aug 22, 2019
1258ed4
remove unused method
lvilya Sep 4, 2019
bfef45f
add the flush flag check
lvilya Sep 4, 2019
0a77127
not working here??
lvilya Sep 4, 2019
ad1636d
not working chnges
lvilya Sep 4, 2019
e314f62
address the comments and improve the excpetion handling logic
lvilya Sep 5, 2019
00d652c
add rubycop config file and run it
lvilya Sep 5, 2019
25b7176
change the oprand
lvilya Sep 5, 2019
ca85153
forgot about one more
lvilya Sep 5, 2019
88f86cc
address comments
lvilya Sep 27, 2019
78e24c6
lints
lvilya Sep 27, 2019
a8c3ab1
change the locking logic ang change linting rules
lvilya Sep 30, 2019
a594976
add lamda for excpetion handling
lvilya Sep 30, 2019
984b4c3
check lock owning
lvilya Oct 1, 2019
14727eb
fix the bug and add the lock to the side messages
lvilya Oct 1, 2019
4395fd7
lints
lvilya Oct 1, 2019
068bc3d
remove redundant nil
lvilya Oct 1, 2019
ad965d4
remove the checking
lvilya Oct 2, 2019
30eaaba
move the falg into lock
lvilya Oct 8, 2019
d94bbca
remove block
lvilya Oct 9, 2019
d8e4ad3
rubocop
lvilya Oct 9, 2019
690fa67
forgot to move another flag swtich into the lock block
lvilya Oct 9, 2019
86cc043
restore the possibility to pass blocks as messages
lvilya Oct 10, 2019
9f86f72
change the logic to read easier
lvilya Oct 10, 2019
443f8d0
update rubocop although it didd not have any effect. rubocop is stupid.
lvilya Oct 10, 2019
cdb6ced
commit
lvilya Oct 17, 2019
1ecf5cb
conflicts
lvilya Oct 17, 2019
e66c44b
sdfsdf
lvilya Oct 17, 2019
4316bf4
sdfsdF
lvilya Oct 17, 2019
5a2ecaf
fix the dep
lvilya Oct 17, 2019
1ac174b
address the comments
lvilya Oct 22, 2019
9d30f80
remove the stray debug message
lvilya Oct 22, 2019
40484a2
gitigone
lvilya Oct 24, 2019
2de8895
remove the block
lvilya Oct 25, 2019
c9a07ff
gitingone
lvilya Oct 26, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# Commonly used screens these days easily fit more than 80 characters.
Metrics/LineLength:
Max: 120

# Too short methods lead to extraction of single-use methods, which can make
# the code easier to read (by naming things), but can also clutter the class
Metrics/MethodLength:
Max: 100

# The guiding principle of classes is SRP, SRP can't be accurately measured by LoC
Metrics/ClassLength:
Max: 1500

Metrics/AbcSize:
Max: 40

Metrics/CyclomaticComplexity:
Max: 40

Metrics/PerceivedComplexity:
Max: 40

# No space makes the method definition shorter and differentiates
# from a regular assignment.

Layout/AccessModifierIndentation:
Enabled: true
IndentationWidth: 4

# Single quotes being faster is hardly measurable and only affects parse time.
# Enforcing double quotes reduces the times where you need to change them
# when introducing an interpolation. Use single quotes only if their semantics
# are needed.
Style/StringLiterals:
EnforcedStyle: double_quotes

# We do not need to support Ruby 1.9, so this is good to use.
Style/SymbolArray:
Enabled: true

# Mixing the styles looks just silly.
Style/HashSyntax:
EnforcedStyle: ruby19_no_mixed_keys

# has_key? and has_value? are far more readable than key? and value?
Style/PreferredHashMethods:
Enabled: false

# String#% is by far the least verbose and only object oriented variant.
Style/FormatString:
EnforcedStyle: percent

Style/CollectionMethods:
Enabled: true
PreferredMethods:
# inject seems more common in the community.
reduce: "inject"

Style/UnneededInterpolation:
Enabled: flase

Style/RescueStandardError:
Enabled: flase

# Either allow this style or don't. Marking it as safe with parenthesis
# is silly. Let's try to live without them for now.
Style/ParenthesesAroundCondition:
AllowSafeAssignment: false
Lint/AssignmentInCondition:
AllowSafeAssignment: false

# A specialized exception class will take one or more arguments and construct the message from it.
# So both variants make sense.
Style/RaiseArgs:
Enabled: false

# Fail is an alias of raise. Avoid aliases, it's more cognitive load for no gain.
# The argument that fail should be used to abort the program is wrong too,
# there's Kernel#abort for that.
Style/SignalException:
EnforcedStyle: only_raise

# Suppressing exceptions can be perfectly fine, and be it to avoid to
# explicitly type nil into the rescue since that's what you want to return,
# or suppressing LoadError for optional dependencies
Lint/HandleExceptions:
Enabled: false

# { ... } for multi-line blocks is okay, follow Weirichs rule instead:
# https://web.archive.org/web/20140221124509/http://onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc
Style/BlockDelimiters:
Enabled: false

# do / end blocks should be used for side effects,
# methods that run a block for side effects and have
# a useful return value are rare, assign the return
# value to a local variable for those cases.
Style/MethodCalledOnDoEndBlock:
Enabled: true

# Enforcing the names of variables? To single letter ones? Just no.
Style/SingleLineBlockParams:
Enabled: false

# Shadowing outer local variables with block parameters is often useful
# to not reinvent a new name for the same thing, it highlights the relation
# between the outer variable and the parameter. The cases where it's actually
# confusing are rare, and usually bad for other reasons already, for example
# because the method is too long.
Lint/ShadowingOuterLocalVariable:
Enabled: false

# Check with yard instead.
Style/Documentation:
Enabled: false

# This is just silly. Calling the argument `other` in all cases makes no sense.
Naming/BinaryOperatorParameterName:
Enabled: false

# There are valid cases, for example debugging Cucumber steps,
# also they'll fail CI anyway
Lint/Debugger:
Enabled: false

# Style preference
Style/MethodDefParentheses:
Enabled: false

Style/TrailingCommaInHashLiteral:
Enabled: false
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.0
2.7.0
137 changes: 55 additions & 82 deletions lib/logdna.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/usr/bin/env ruby
# encoding: utf-8
# frozen_string_literal: true

# require 'singleton'
require 'socket'
require 'uri'
require_relative 'logdna/client.rb'
require_relative 'logdna/resources.rb'
require "socket"
require "uri"
require_relative "logdna/client.rb"
require_relative "logdna/resources.rb"
require_relative "logdna/version.rb"
module Logdna
class ValidURLRequired < ArgumentError; end
class MaxLengthExceeded < ArgumentError; end
Expand All @@ -15,54 +17,28 @@ class Ruby < ::Logger
Logger::TRACE = 5
attr_accessor :level, :app, :env, :meta

def initialize(key, opts={})
@app = opts[:app] || 'default'
@level = opts[:level] || 'INFO'
def initialize(key, opts = {})
@app = opts[:app] || "default"
@level = opts[:level] || "INFO"
@env = opts[:env]
@meta = opts[:meta]
@@client = nil unless defined? @@client

endpoint = opts[:endpoint] || Resources::ENDPOINT
hostname = opts[:hostname] || Socket.gethostname
ip = opts.key?(:ip) ? "&ip=#{opts[:ip]}" : ''
mac = opts.key?(:mac) ? "&mac=#{opts[:mac]}" : ''
url = "#{endpoint}?hostname=#{hostname}#{mac}#{ip}"

begin
if (hostname.size > Resources::MAX_INPUT_LENGTH || @app.size > Resources::MAX_INPUT_LENGTH )
raise MaxLengthExceeded.new
end
rescue MaxLengthExceeded => e
if hostname.size > Resources::MAX_INPUT_LENGTH || @app.size > Resources::MAX_INPUT_LENGTH
puts "Hostname or Appname is over #{Resources::MAX_INPUT_LENGTH} characters"
handle_exception(e)
return
end

begin
uri = URI(url)
rescue URI::ValidURIRequired => e
puts "Invalid URL Endpoint: #{url}"
handle_exception(e)
return
end

begin
request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json')
request.basic_auth 'username', key
request[:'user-agent'] = opts[:'user-agent'] || "ruby/#{LogDNA::VERSION}"
rescue => e
handle_exception(e)
return
end

@@client = Logdna::Client.new(request, uri, opts)
end
ip = opts.key?(:ip) ? "&ip=#{opts[:ip]}" : ""
mac = opts.key?(:mac) ? "&mac=#{opts[:mac]}" : ""
url = "#{endpoint}?hostname=#{hostname}#{mac}#{ip}"
uri = URI(url)

def handle_exception(e)
exception_message = e.message
exception_backtrace = e.backtrace
# NOTE: should log with Ruby logger?
puts exception_message
request = Net::HTTP::Post.new(uri.request_uri, "Content-Type" => "application/json")
request.basic_auth("username", key)
request[:'user-agent'] = opts[:'user-agent'] || "ruby/#{LogDNA::VERSION}"
@client = Logdna::Client.new(request, uri, opts)
end

def default_opts
Expand All @@ -83,77 +59,74 @@ def level=(value)
@level = value
end

def log(msg=nil, opts={})
loggerExist?
message = msg
message = yield if msg.nil? && block_given?
@response = @@client.buffer(message, default_opts.merge(opts).merge({
timestamp: (Time.now.to_f * 1000).to_i
}))
'Saved'
def log(message = nil, opts = {})
if message.nil? && block_given?
message = yield
end
if message.nil?
puts "provide either a message or block"
return
end
message = message.to_s.encode("UTF-8")
@client.write_to_buffer(message, default_opts.merge(opts).merge(
timestamp: (Time.now.to_f * 1000).to_i
))
end

Resources::LOG_LEVELS.each do |lvl|
name = lvl.downcase

define_method name do |msg=nil, opts={}, &block|
self.log(msg, opts.merge({
level: lvl,
}), &block)
define_method name do |msg = nil, opts = {}, &block|
self.log(msg, opts.merge(
level: lvl
), &block)
end

define_method "#{name}?" do
return Resources::LOG_LEVELS[self.level] == lvl if self.level.is_a? Numeric
return Resources::LOG_LEVELS[self.level] == lvl if level.is_a? Numeric

self.level == lvl
end
end

def clear
@app = 'default'
@level = 'INFO'
@app = "default"
@level = "INFO"
@env = nil
@meta = nil
end

def loggerExist?
if @@client.nil?
puts "Logger Not Initialized Yet"
close
end
end

def <<(msg=nil, opts={})
self.log(msg, opts.merge({
level: '',
}))
def <<(msg = nil, opts = {})
log(msg, opts.merge(
level: ""
))
end

def add(*arg)
def add(*_arg)
puts "add not supported in LogDNA logger"
return false
false
end

def unknown(msg=nil, opts={})
self.log(msg, opts.merge({
level: 'UNKNOWN',
}))
def unknown(msg = nil, opts = {})
log(msg, opts.merge(
level: "UNKNOWN"
))
end

def datetime_format(*arg)
def datetime_format(*_arg)
puts "datetime_format not supported in LogDNA logger"
return false
false
end


def close
if defined? @@client and !@@client.nil?
@@client.exitout()
if !@client.nil?
@client.exitout
end
end

at_exit do
if defined? @@client and !@@client.nil?
@@client.exitout()
if !@client.nil?
@client.exitout
end
end
end
Expand Down
Loading