Skip to content

Commit

Permalink
🐎 🐣 Rewrite logger.
Browse files Browse the repository at this point in the history
This logger uses the Cinch events rather than a command that matches absolutely every possible message. Aside from the obvious performance improvement, here are some functional improvements:
* Supports /me ACTIONs and NOTICEs.
* No longer logs messages with no user.
* Still logs private and public messages separately.
* The log file is formatted much cleaner.
* The time is checked every minute to make sure it is the same day as it was last time it checked. When it is a new day (past midnight), it starts a new log.
Unfortunately, it still doesnt log the bots own messages (#73).
This commit resolves half of #73.
  • Loading branch information
elifoster committed Dec 18, 2015
1 parent cd1ab41 commit 2386cd0
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 53 deletions.
23 changes: 2 additions & 21 deletions src/littlehelper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ module LittleHelper
Plugins::Commands::Weather,
Plugins::Commands::BanInfo,
Plugins::Commands::IssueLink,
Plugins::Commands::Log,
Plugins::Commands::MoveCategory,
Plugins::Commands::MoveGeneral,
Plugins::Commands::CategoryMembers,
Plugins::Commands::ChangeCategory,
Plugins::Commands::GetAbbreviation
Plugins::Commands::GetAbbreviation,
Plugins::Logger
]
c.plugins.prefix = /^\$/

Expand Down Expand Up @@ -100,25 +100,6 @@ def wiki_login
end

def run
log
BOT.start
end

def log
time = Time.now
CHANNELS.each do |c|
file_name = "#{Dir.pwd}/logs/#{c}-#{time.year}-#{time.month}-#{time.day}.log"
if File.file?(file_name)
open(file_name, 'a') do |f|
f.puts("\n\n## Logging start at #{time.hour}:#{time.min}:#{time.sec} " \
"#{time.zone}\n")
end
else
File.new(file_name, 'a') do |f|
f.puts("## Logging start at #{time.hour}:#{time.min}:#{time.sec} " \
"#{time.zone}\n")
end
end
end
end
end
32 changes: 0 additions & 32 deletions src/plugins/commands/log.rb

This file was deleted.

122 changes: 122 additions & 0 deletions src/plugins/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
require 'cinch'

module Plugins
class Logger
include Cinch::Plugin

listen_to(:connect, method: :setup)
listen_to(:disconnect, method: :cleanup)
listen_to(:channel, method: :log_public_message)
listen_to(:private, method: :log_private_message)
listen_to(:action, method: :log_action)
listen_to(:notice, method: :log_notice)
timer(60, method: :check_time)

# Prepares for logging. Creates all the format strings. Creates/opens all
# the channel files. User files will be created when necessary. Unsure
# about the parameters.
def setup(*)
@file_format = "#{Dir.pwd}/logs/%{channel}-%{year}-%{month}-%{day}.log"
@time_format = '%H:%M:%S'
@log_format = "[%{time}] <%{nick}>: %{msg}\n"
@action_format = "[%{time}] %{nick} %{action}\n"
@notice_format = "[%{time}] *%{nick}* %{notice}\n"
@last_time_check = Time.now
@log_files = {}
LittleHelper::CHANNELS.each do |channel|
name = format(@file_format,
channel: channel,
year: @last_time_check.year,
month: @last_time_check.month,
day: @last_time_check.day)
file = File.file?(name) ? File.open(name, 'a') : File.new(name, 'a')
@log_files[channel] = file
end
end

# Clears up all the stuff. Closes all the files and clears the array. Unsure
# about the parameters.
def cleanup(*)
@log_files.map(&:close)
@log_files.clear
end

# Checks whether the current day is the same as the time when it was last
# set. It is set in #check_time as well as #setup. Called every 60 seconds
def check_time
time = Time.now
cleanup if time.day != @last_time_check.day
@last_time_check = time
setup
end

# Logs a public message that is not an ACTION or a NOTICE.
# @param msg [Cinch::Message] The message object sent by the event.
def log_public_message(msg)
return if msg.action?
return if msg.command == 'NOTICE'
hash = {
nick: msg.user.name,
msg: msg.message
}
log_general(msg.channel, hash)
end

# Logs a private message that is not an action or sent by the network.
# @param msg [Cinch::Message] See #log_public_message
def log_private_message(msg)
return if msg.action?
return if msg.user.nil?
hash = {
nick: msg.user.name,
msg: msg.message
}
log_general(msg.user.authname, hash)
end

# Logs an ACTION or /me.
# @param msg [Cinch::Message] See #log_public_message
def log_action(msg)
prefix = msg.channel.nil? ? msg.user.authname : msg.channel
hash = {
nick: msg.user.name,
action: msg.action_message
}
log_general(prefix, hash, @action_format)
end

# Logs a NOTICE created by a user.
# @param msg [Cinch::Message] See #log_public_message
def log_notice(msg)
return if msg.user.nil?
prefix = msg.channel.nil? ? msg.user.authname : msg.channel
hash = {
nick: msg.user.name,
notice: msg.message
}
log_general(prefix, hash, @notice_format)
end

private

# Logs to a file based on the parameters passed.
# @param prefix [String] The prefix to the file name. This is typically
# either the channel name or username.
# @param format_hash [Hash] The hash used to format the string. The time
# key will never need to be set manually, as it is done first thing in the
# method. Just set all the other options needed by the format.
# @param format_str [String] The string to format. Use one of the values in
# #setup.
def log_general(prefix, format_hash, format_str = @log_format)
format_hash[:time] = Time.now.strftime(@time_format)
name = format(@file_format,
channel: prefix,
year: @last_time_check.year,
month: @last_time_check.month,
day: @last_time_check.day)
file = File.file?(name) ? File.open(name, 'a') : File.new(name, 'a')
@log_files[prefix] = file
@log_files[prefix].puts(format(format_str, format_hash))
end
end
end

0 comments on commit 2386cd0

Please sign in to comment.