Skip to content

Instantly share code, notes, and snippets.

@loganathan-s

loganathan-s/logger.rb

Last active Aug 29, 2015
Embed
What would you like to do?
Remote syslog
require 'socket'
require 'syslog_protocol'
module RemoteSyslogLogger
class UdpSender
def initialize(remote_hostname, remote_port, options = {})
@remote_hostname = remote_hostname
@remote_port = remote_port
@whinyerrors = options[:whinyerrors]
@socket = UDPSocket.new
@packet = SyslogProtocol::Packet.new
local_hostname = options[:local_hostname] || (Socket.gethostname rescue `hostname`.chomp)
local_hostname = 'localhost' if local_hostname.nil? || local_hostname.empty?
@packet.hostname = local_hostname
@packet.facility = options[:facility] || 'user'
@packet.severity = options[:severity] || 'notice'
@packet.tag = options[:program] || "#{File.basename($0)}[#{$$}]"
end
def transmit(message)
#logger = Logger.new('log/app.log', 10, 100.megabytes)
message.split(/\r?\n/).each do |line|
begin
next if line =~ /^\s*$/
# LOG IN TO LOCAL MACHINE
File.open("log/applicatiob.log", "a") {|file| file.puts(line)}
packet = @packet.dup
packet.content = line
# LOG IN TO REMOTE SYSLOG
@socket.send(packet.assemble, 0, @remote_hostname, @remote_port)
rescue
$stderr.puts "#{self.class} error: #{$!.class}: #{$!}\nOriginal message: #{line}"
raise if @whinyerrors
end
end
end
# Make this act a little bit like an `IO` object
alias_method :write, :transmit
def close
@socket.close
end
end
end
LogAPP::Application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
config.time_zone = 'UTC'
#config.logger = ::Logger.new('log/production.log')
config.logger = RemoteSyslogLogger.new('SYSLOG MACHINE IP', 514, :program => "RAILS-SYSLOG")
config.logger.formatter = proc do |severity, datetime, progname, msg|
"#{severity[0..0]}, [#{datetime.utc.strftime("%Y-%m-%d %H:%M:%S.%6N %Z")} ##{$$}] #{severity} -- #{progname} : #{msg}\n"
end
end
Rails::Rack::Logger.class_eval do
def started_request_message(request)
'Started %s "%s" for %s at %s' % [
request.request_method,
request.filtered_path,
request.ip,
Time.now.utc ]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment