lib/log4r/outputter/evernoteoutputter.rb in log4ever-0.1.4 vs lib/log4r/outputter/evernoteoutputter.rb in log4ever-0.1.5
- old
+ new
@@ -11,54 +11,53 @@
class EvernoteOutputter < Outputter
def initialize(_name, hash = {})
super(_name, hash)
validate(hash)
- end
-
- # synchronize note
- def sync
- @note = @evernote.note(@notebook)
- @tag = @evernote.tag(@note)
+ evernote = Log4ever::Evernote.new(@auth_token, @is_sandbox)
+ @notebook = evernote.notebook.get(@notebook_name, @stack_name)
+ @note = evernote.note(@notebook)
+ @tag = evernote.tag(@note)
@tag.names = @tags
- set_maxsize(@hash) # for rolling
- set_shift_age(@hash) # for rolling
+ set_maxsize(hash) # for rolling
+ set_shift_age(hash) # for rolling
+ @hash = hash
end
# validation of evernote parameters
def validate(hash)
- is_sandbox = hash[:sandbox] || hash['sandbox'] || false
- raise ArgumentError, "Sandbox must be type of boolean" unless is_sandbox == false || is_sandbox == true
+ @is_sandbox = hash[:sandbox] || hash['sandbox'] || false
+ raise ArgumentError, "Sandbox must be type of boolean" unless @is_sandbox == false || @is_sandbox == true
@auth_token = hash[:auth_token] || hash['auth_token'] || ""
raise ArgumentError, "Must specify from auth token" if @auth_token.empty?
- notebook_name = to_utf8(hash[:notebook] || hash['notebook'] || "")
- raise ArgumentError, "Must specify from notebook" if notebook_name.empty?
- stack_name = to_utf8(hash[:stack] || hash['stack'])
- @evernote = Log4ever::Evernote.new(@auth_token, is_sandbox)
+ @notebook_name = to_utf8(hash[:notebook] || hash['notebook'] || "")
+ raise ArgumentError, "Must specify from notebook" if @notebook_name.empty?
+ @stack_name = to_utf8(hash[:stack] || hash['stack'])
@tags = to_utf8(hash[:tags] || hash['tags'] || [])
- notebook = @evernote.notebook
- @notebook = notebook.get(notebook_name, stack_name)
- @hash = hash
- sync
end
def canonical_log(logevent); super end
+ # sync
+ def sync_note
+ @note.get!
+ update_maxtime(@hash) # update rolling status
+ end
+
# write log
def write(content)
- sync
if note_size_requires_roll? || time_requires_roll? || different_tag?
create_log(content)
+ sync_note
else
update_log(content)
end
end
private
# write log to note
def create_log(content)
- @note.clear
@note.title = to_utf8(@name) + " - " + Time.now.strftime("%Y-%m-%d %H:%M:%S")
@note.tags = @tag.get
@note.content = to_utf8(content)
@note.create
Logger.log_internal { "Create note: #{@note.guid}" }
@@ -72,11 +71,11 @@
Logger.log_internal { "Update note: #{@note.guid}" }
end
# more expensive, only for startup
def note_size_requires_roll?
- @note.size == 0 || (@maxsize > 0 && @note.size >= @maxsize)
+ @note.size == 0 || (@maxsize > 0 && @note.size > @maxsize)
end
# whether or not to rotate
def time_requires_roll?
!@endTime.nil? && Time.now.to_i >= @endTime
@@ -142,9 +141,11 @@
@endTime = next_month.to_i
end
end
end
end
+
+ alias_method :update_maxtime, :set_shift_age
# encode for evernote internal charset
# convert character encoding to UTF-8 from Shift_JIS or EUC-JP
def to_utf8(mixed)
if mixed.kind_of? Array
\ No newline at end of file