Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: logger.rb
# logger.rb - simple logging utility
[0] Fix | Delete
# Copyright (C) 2000-2003, 2005 NAKAMURA, Hiroshi <nakahiro@sarion.co.jp>.
[1] Fix | Delete
[2] Fix | Delete
require 'monitor'
[3] Fix | Delete
[4] Fix | Delete
# Simple logging utility.
[5] Fix | Delete
#
[6] Fix | Delete
# Author:: NAKAMURA, Hiroshi <nakahiro@sarion.co.jp>
[7] Fix | Delete
# Documentation:: NAKAMURA, Hiroshi and Gavin Sinclair
[8] Fix | Delete
# License::
[9] Fix | Delete
# You can redistribute it and/or modify it under the same terms of Ruby's
[10] Fix | Delete
# license; either the dual license version in 2003, or any later version.
[11] Fix | Delete
# Revision:: $Id: logger.rb 31806 2011-05-30 02:08:57Z nahi $
[12] Fix | Delete
#
[13] Fix | Delete
# == Description
[14] Fix | Delete
#
[15] Fix | Delete
# The Logger class provides a simple but sophisticated logging utility that
[16] Fix | Delete
# anyone can use because it's included in the Ruby 1.8.x standard library.
[17] Fix | Delete
#
[18] Fix | Delete
# The HOWTOs below give a code-based overview of Logger's usage, but the basic
[19] Fix | Delete
# concept is as follows. You create a Logger object (output to a file or
[20] Fix | Delete
# elsewhere), and use it to log messages. The messages will have varying
[21] Fix | Delete
# levels (+info+, +error+, etc), reflecting their varying importance. The
[22] Fix | Delete
# levels, and their meanings, are:
[23] Fix | Delete
#
[24] Fix | Delete
# +FATAL+:: an unhandleable error that results in a program crash
[25] Fix | Delete
# +ERROR+:: a handleable error condition
[26] Fix | Delete
# +WARN+:: a warning
[27] Fix | Delete
# +INFO+:: generic (useful) information about system operation
[28] Fix | Delete
# +DEBUG+:: low-level information for developers
[29] Fix | Delete
#
[30] Fix | Delete
# So each message has a level, and the Logger itself has a level, which acts
[31] Fix | Delete
# as a filter, so you can control the amount of information emitted from the
[32] Fix | Delete
# logger without having to remove actual messages.
[33] Fix | Delete
#
[34] Fix | Delete
# For instance, in a production system, you may have your logger(s) set to
[35] Fix | Delete
# +INFO+ (or +WARN+ if you don't want the log files growing large with
[36] Fix | Delete
# repetitive information). When you are developing it, though, you probably
[37] Fix | Delete
# want to know about the program's internal state, and would set them to
[38] Fix | Delete
# +DEBUG+.
[39] Fix | Delete
#
[40] Fix | Delete
# === Example
[41] Fix | Delete
#
[42] Fix | Delete
# A simple example demonstrates the above explanation:
[43] Fix | Delete
#
[44] Fix | Delete
# log = Logger.new(STDOUT)
[45] Fix | Delete
# log.level = Logger::WARN
[46] Fix | Delete
#
[47] Fix | Delete
# log.debug("Created logger")
[48] Fix | Delete
# log.info("Program started")
[49] Fix | Delete
# log.warn("Nothing to do!")
[50] Fix | Delete
#
[51] Fix | Delete
# begin
[52] Fix | Delete
# File.each_line(path) do |line|
[53] Fix | Delete
# unless line =~ /^(\w+) = (.*)$/
[54] Fix | Delete
# log.error("Line in wrong format: #{line}")
[55] Fix | Delete
# end
[56] Fix | Delete
# end
[57] Fix | Delete
# rescue => err
[58] Fix | Delete
# log.fatal("Caught exception; exiting")
[59] Fix | Delete
# log.fatal(err)
[60] Fix | Delete
# end
[61] Fix | Delete
#
[62] Fix | Delete
# Because the Logger's level is set to +WARN+, only the warning, error, and
[63] Fix | Delete
# fatal messages are recorded. The debug and info messages are silently
[64] Fix | Delete
# discarded.
[65] Fix | Delete
#
[66] Fix | Delete
# === Features
[67] Fix | Delete
#
[68] Fix | Delete
# There are several interesting features that Logger provides, like
[69] Fix | Delete
# auto-rolling of log files, setting the format of log messages, and
[70] Fix | Delete
# specifying a program name in conjunction with the message. The next section
[71] Fix | Delete
# shows you how to achieve these things.
[72] Fix | Delete
#
[73] Fix | Delete
#
[74] Fix | Delete
# == HOWTOs
[75] Fix | Delete
#
[76] Fix | Delete
# === How to create a logger
[77] Fix | Delete
#
[78] Fix | Delete
# The options below give you various choices, in more or less increasing
[79] Fix | Delete
# complexity.
[80] Fix | Delete
#
[81] Fix | Delete
# 1. Create a logger which logs messages to STDERR/STDOUT.
[82] Fix | Delete
#
[83] Fix | Delete
# logger = Logger.new(STDERR)
[84] Fix | Delete
# logger = Logger.new(STDOUT)
[85] Fix | Delete
#
[86] Fix | Delete
# 2. Create a logger for the file which has the specified name.
[87] Fix | Delete
#
[88] Fix | Delete
# logger = Logger.new('logfile.log')
[89] Fix | Delete
#
[90] Fix | Delete
# 3. Create a logger for the specified file.
[91] Fix | Delete
#
[92] Fix | Delete
# file = File.open('foo.log', File::WRONLY | File::APPEND)
[93] Fix | Delete
# # To create new (and to remove old) logfile, add File::CREAT like;
[94] Fix | Delete
# # file = open('foo.log', File::WRONLY | File::APPEND | File::CREAT)
[95] Fix | Delete
# logger = Logger.new(file)
[96] Fix | Delete
#
[97] Fix | Delete
# 4. Create a logger which ages logfile once it reaches a certain size. Leave
[98] Fix | Delete
# 10 "old log files" and each file is about 1,024,000 bytes.
[99] Fix | Delete
#
[100] Fix | Delete
# logger = Logger.new('foo.log', 10, 1024000)
[101] Fix | Delete
#
[102] Fix | Delete
# 5. Create a logger which ages logfile daily/weekly/monthly.
[103] Fix | Delete
#
[104] Fix | Delete
# logger = Logger.new('foo.log', 'daily')
[105] Fix | Delete
# logger = Logger.new('foo.log', 'weekly')
[106] Fix | Delete
# logger = Logger.new('foo.log', 'monthly')
[107] Fix | Delete
#
[108] Fix | Delete
# === How to log a message
[109] Fix | Delete
#
[110] Fix | Delete
# Notice the different methods (+fatal+, +error+, +info+) being used to log
[111] Fix | Delete
# messages of various levels. Other methods in this family are +warn+ and
[112] Fix | Delete
# +debug+. +add+ is used below to log a message of an arbitrary (perhaps
[113] Fix | Delete
# dynamic) level.
[114] Fix | Delete
#
[115] Fix | Delete
# 1. Message in block.
[116] Fix | Delete
#
[117] Fix | Delete
# logger.fatal { "Argument 'foo' not given." }
[118] Fix | Delete
#
[119] Fix | Delete
# 2. Message as a string.
[120] Fix | Delete
#
[121] Fix | Delete
# logger.error "Argument #{ @foo } mismatch."
[122] Fix | Delete
#
[123] Fix | Delete
# 3. With progname.
[124] Fix | Delete
#
[125] Fix | Delete
# logger.info('initialize') { "Initializing..." }
[126] Fix | Delete
#
[127] Fix | Delete
# 4. With severity.
[128] Fix | Delete
#
[129] Fix | Delete
# logger.add(Logger::FATAL) { 'Fatal error!' }
[130] Fix | Delete
#
[131] Fix | Delete
# === How to close a logger
[132] Fix | Delete
#
[133] Fix | Delete
# logger.close
[134] Fix | Delete
#
[135] Fix | Delete
# === Setting severity threshold
[136] Fix | Delete
#
[137] Fix | Delete
# 1. Original interface.
[138] Fix | Delete
#
[139] Fix | Delete
# logger.sev_threshold = Logger::WARN
[140] Fix | Delete
#
[141] Fix | Delete
# 2. Log4r (somewhat) compatible interface.
[142] Fix | Delete
#
[143] Fix | Delete
# logger.level = Logger::INFO
[144] Fix | Delete
#
[145] Fix | Delete
# DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
[146] Fix | Delete
#
[147] Fix | Delete
#
[148] Fix | Delete
# == Format
[149] Fix | Delete
#
[150] Fix | Delete
# Log messages are rendered in the output stream in a certain format. The
[151] Fix | Delete
# default format and a sample are shown below:
[152] Fix | Delete
#
[153] Fix | Delete
# Log format:
[154] Fix | Delete
# SeverityID, [Date Time mSec #pid] SeverityLabel -- ProgName: message
[155] Fix | Delete
#
[156] Fix | Delete
# Log sample:
[157] Fix | Delete
# I, [Wed Mar 03 02:34:24 JST 1999 895701 #19074] INFO -- Main: info.
[158] Fix | Delete
#
[159] Fix | Delete
# You may change the date and time format in this manner:
[160] Fix | Delete
#
[161] Fix | Delete
# logger.datetime_format = "%Y-%m-%d %H:%M:%S"
[162] Fix | Delete
# # e.g. "2004-01-03 00:54:26"
[163] Fix | Delete
#
[164] Fix | Delete
# There is currently no supported way to change the overall format, but you may
[165] Fix | Delete
# have some luck hacking the Format constant.
[166] Fix | Delete
#
[167] Fix | Delete
[168] Fix | Delete
[169] Fix | Delete
class Logger
[170] Fix | Delete
VERSION = "1.2.6"
[171] Fix | Delete
ProgName = "#{File.basename(__FILE__)}/#{VERSION}"
[172] Fix | Delete
[173] Fix | Delete
class Error < RuntimeError; end
[174] Fix | Delete
class ShiftingError < Error; end
[175] Fix | Delete
[176] Fix | Delete
# Logging severity.
[177] Fix | Delete
module Severity
[178] Fix | Delete
DEBUG = 0
[179] Fix | Delete
INFO = 1
[180] Fix | Delete
WARN = 2
[181] Fix | Delete
ERROR = 3
[182] Fix | Delete
FATAL = 4
[183] Fix | Delete
UNKNOWN = 5
[184] Fix | Delete
end
[185] Fix | Delete
include Severity
[186] Fix | Delete
[187] Fix | Delete
# Logging severity threshold (e.g. <tt>Logger::INFO</tt>).
[188] Fix | Delete
attr_accessor :level
[189] Fix | Delete
[190] Fix | Delete
# Logging program name.
[191] Fix | Delete
attr_accessor :progname
[192] Fix | Delete
[193] Fix | Delete
# Logging date-time format (string passed to +strftime+).
[194] Fix | Delete
def datetime_format=(datetime_format)
[195] Fix | Delete
@default_formatter.datetime_format = datetime_format
[196] Fix | Delete
end
[197] Fix | Delete
[198] Fix | Delete
def datetime_format
[199] Fix | Delete
@default_formatter.datetime_format
[200] Fix | Delete
end
[201] Fix | Delete
[202] Fix | Delete
# Logging formatter. formatter#call is invoked with 4 arguments; severity,
[203] Fix | Delete
# time, progname and msg for each log. Bear in mind that time is a Time and
[204] Fix | Delete
# msg is an Object that user passed and it could not be a String. It is
[205] Fix | Delete
# expected to return a logdev#write-able Object. Default formatter is used
[206] Fix | Delete
# when no formatter is set.
[207] Fix | Delete
attr_accessor :formatter
[208] Fix | Delete
[209] Fix | Delete
alias sev_threshold level
[210] Fix | Delete
alias sev_threshold= level=
[211] Fix | Delete
[212] Fix | Delete
# Returns +true+ iff the current severity level allows for the printing of
[213] Fix | Delete
# +DEBUG+ messages.
[214] Fix | Delete
def debug?; @level <= DEBUG; end
[215] Fix | Delete
[216] Fix | Delete
# Returns +true+ iff the current severity level allows for the printing of
[217] Fix | Delete
# +INFO+ messages.
[218] Fix | Delete
def info?; @level <= INFO; end
[219] Fix | Delete
[220] Fix | Delete
# Returns +true+ iff the current severity level allows for the printing of
[221] Fix | Delete
# +WARN+ messages.
[222] Fix | Delete
def warn?; @level <= WARN; end
[223] Fix | Delete
[224] Fix | Delete
# Returns +true+ iff the current severity level allows for the printing of
[225] Fix | Delete
# +ERROR+ messages.
[226] Fix | Delete
def error?; @level <= ERROR; end
[227] Fix | Delete
[228] Fix | Delete
# Returns +true+ iff the current severity level allows for the printing of
[229] Fix | Delete
# +FATAL+ messages.
[230] Fix | Delete
def fatal?; @level <= FATAL; end
[231] Fix | Delete
[232] Fix | Delete
#
[233] Fix | Delete
# === Synopsis
[234] Fix | Delete
#
[235] Fix | Delete
# Logger.new(name, shift_age = 7, shift_size = 1048576)
[236] Fix | Delete
# Logger.new(name, shift_age = 'weekly')
[237] Fix | Delete
#
[238] Fix | Delete
# === Args
[239] Fix | Delete
#
[240] Fix | Delete
# +logdev+::
[241] Fix | Delete
# The log device. This is a filename (String) or IO object (typically
[242] Fix | Delete
# +STDOUT+, +STDERR+, or an open file).
[243] Fix | Delete
# +shift_age+::
[244] Fix | Delete
# Number of old log files to keep, *or* frequency of rotation (+daily+,
[245] Fix | Delete
# +weekly+ or +monthly+).
[246] Fix | Delete
# +shift_size+::
[247] Fix | Delete
# Maximum logfile size (only applies when +shift_age+ is a number).
[248] Fix | Delete
#
[249] Fix | Delete
# === Description
[250] Fix | Delete
#
[251] Fix | Delete
# Create an instance.
[252] Fix | Delete
#
[253] Fix | Delete
def initialize(logdev, shift_age = 0, shift_size = 1048576)
[254] Fix | Delete
@progname = nil
[255] Fix | Delete
@level = DEBUG
[256] Fix | Delete
@default_formatter = Formatter.new
[257] Fix | Delete
@formatter = nil
[258] Fix | Delete
@logdev = nil
[259] Fix | Delete
if logdev
[260] Fix | Delete
@logdev = LogDevice.new(logdev, :shift_age => shift_age,
[261] Fix | Delete
:shift_size => shift_size)
[262] Fix | Delete
end
[263] Fix | Delete
end
[264] Fix | Delete
[265] Fix | Delete
#
[266] Fix | Delete
# === Synopsis
[267] Fix | Delete
#
[268] Fix | Delete
# Logger#add(severity, message = nil, progname = nil) { ... }
[269] Fix | Delete
#
[270] Fix | Delete
# === Args
[271] Fix | Delete
#
[272] Fix | Delete
# +severity+::
[273] Fix | Delete
# Severity. Constants are defined in Logger namespace: +DEBUG+, +INFO+,
[274] Fix | Delete
# +WARN+, +ERROR+, +FATAL+, or +UNKNOWN+.
[275] Fix | Delete
# +message+::
[276] Fix | Delete
# The log message. A String or Exception.
[277] Fix | Delete
# +progname+::
[278] Fix | Delete
# Program name string. Can be omitted. Treated as a message if no +message+ and
[279] Fix | Delete
# +block+ are given.
[280] Fix | Delete
# +block+::
[281] Fix | Delete
# Can be omitted. Called to get a message string if +message+ is nil.
[282] Fix | Delete
#
[283] Fix | Delete
# === Return
[284] Fix | Delete
#
[285] Fix | Delete
# +true+ if successful, +false+ otherwise.
[286] Fix | Delete
#
[287] Fix | Delete
# When the given severity is not high enough (for this particular logger), log
[288] Fix | Delete
# no message, and return +true+.
[289] Fix | Delete
#
[290] Fix | Delete
# === Description
[291] Fix | Delete
#
[292] Fix | Delete
# Log a message if the given severity is high enough. This is the generic
[293] Fix | Delete
# logging method. Users will be more inclined to use #debug, #info, #warn,
[294] Fix | Delete
# #error, and #fatal.
[295] Fix | Delete
#
[296] Fix | Delete
# <b>Message format</b>: +message+ can be any object, but it has to be
[297] Fix | Delete
# converted to a String in order to log it. Generally, +inspect+ is used
[298] Fix | Delete
# if the given object is not a String.
[299] Fix | Delete
# A special case is an +Exception+ object, which will be printed in detail,
[300] Fix | Delete
# including message, class, and backtrace. See #msg2str for the
[301] Fix | Delete
# implementation if required.
[302] Fix | Delete
#
[303] Fix | Delete
# === Bugs
[304] Fix | Delete
#
[305] Fix | Delete
# * Logfile is not locked.
[306] Fix | Delete
# * Append open does not need to lock file.
[307] Fix | Delete
# * But on the OS which supports multi I/O, records possibly be mixed.
[308] Fix | Delete
#
[309] Fix | Delete
def add(severity, message = nil, progname = nil, &block)
[310] Fix | Delete
severity ||= UNKNOWN
[311] Fix | Delete
if @logdev.nil? or severity < @level
[312] Fix | Delete
return true
[313] Fix | Delete
end
[314] Fix | Delete
progname ||= @progname
[315] Fix | Delete
if message.nil?
[316] Fix | Delete
if block_given?
[317] Fix | Delete
message = yield
[318] Fix | Delete
else
[319] Fix | Delete
message = progname
[320] Fix | Delete
progname = @progname
[321] Fix | Delete
end
[322] Fix | Delete
end
[323] Fix | Delete
@logdev.write(
[324] Fix | Delete
format_message(format_severity(severity), Time.now, progname, message))
[325] Fix | Delete
true
[326] Fix | Delete
end
[327] Fix | Delete
alias log add
[328] Fix | Delete
[329] Fix | Delete
#
[330] Fix | Delete
# Dump given message to the log device without any formatting. If no log
[331] Fix | Delete
# device exists, return +nil+.
[332] Fix | Delete
#
[333] Fix | Delete
def <<(msg)
[334] Fix | Delete
unless @logdev.nil?
[335] Fix | Delete
@logdev.write(msg)
[336] Fix | Delete
end
[337] Fix | Delete
end
[338] Fix | Delete
[339] Fix | Delete
#
[340] Fix | Delete
# Log a +DEBUG+ message.
[341] Fix | Delete
#
[342] Fix | Delete
# See #info for more information.
[343] Fix | Delete
#
[344] Fix | Delete
def debug(progname = nil, &block)
[345] Fix | Delete
add(DEBUG, nil, progname, &block)
[346] Fix | Delete
end
[347] Fix | Delete
[348] Fix | Delete
#
[349] Fix | Delete
# Log an +INFO+ message.
[350] Fix | Delete
#
[351] Fix | Delete
# The message can come either from the +progname+ argument or the +block+. If
[352] Fix | Delete
# both are provided, then the +block+ is used as the message, and +progname+
[353] Fix | Delete
# is used as the program name.
[354] Fix | Delete
#
[355] Fix | Delete
# === Examples
[356] Fix | Delete
#
[357] Fix | Delete
# logger.info("MainApp") { "Received connection from #{ip}" }
[358] Fix | Delete
# # ...
[359] Fix | Delete
# logger.info "Waiting for input from user"
[360] Fix | Delete
# # ...
[361] Fix | Delete
# logger.info { "User typed #{input}" }
[362] Fix | Delete
#
[363] Fix | Delete
# You'll probably stick to the second form above, unless you want to provide a
[364] Fix | Delete
# program name (which you can do with <tt>Logger#progname=</tt> as well).
[365] Fix | Delete
#
[366] Fix | Delete
# === Return
[367] Fix | Delete
#
[368] Fix | Delete
# See #add.
[369] Fix | Delete
#
[370] Fix | Delete
def info(progname = nil, &block)
[371] Fix | Delete
add(INFO, nil, progname, &block)
[372] Fix | Delete
end
[373] Fix | Delete
[374] Fix | Delete
#
[375] Fix | Delete
# Log a +WARN+ message.
[376] Fix | Delete
#
[377] Fix | Delete
# See #info for more information.
[378] Fix | Delete
#
[379] Fix | Delete
def warn(progname = nil, &block)
[380] Fix | Delete
add(WARN, nil, progname, &block)
[381] Fix | Delete
end
[382] Fix | Delete
[383] Fix | Delete
#
[384] Fix | Delete
# Log an +ERROR+ message.
[385] Fix | Delete
#
[386] Fix | Delete
# See #info for more information.
[387] Fix | Delete
#
[388] Fix | Delete
def error(progname = nil, &block)
[389] Fix | Delete
add(ERROR, nil, progname, &block)
[390] Fix | Delete
end
[391] Fix | Delete
[392] Fix | Delete
#
[393] Fix | Delete
# Log a +FATAL+ message.
[394] Fix | Delete
#
[395] Fix | Delete
# See #info for more information.
[396] Fix | Delete
#
[397] Fix | Delete
def fatal(progname = nil, &block)
[398] Fix | Delete
add(FATAL, nil, progname, &block)
[399] Fix | Delete
end
[400] Fix | Delete
[401] Fix | Delete
#
[402] Fix | Delete
# Log an +UNKNOWN+ message. This will be printed no matter what the logger
[403] Fix | Delete
# level.
[404] Fix | Delete
#
[405] Fix | Delete
# See #info for more information.
[406] Fix | Delete
#
[407] Fix | Delete
def unknown(progname = nil, &block)
[408] Fix | Delete
add(UNKNOWN, nil, progname, &block)
[409] Fix | Delete
end
[410] Fix | Delete
[411] Fix | Delete
#
[412] Fix | Delete
# Close the logging device.
[413] Fix | Delete
#
[414] Fix | Delete
def close
[415] Fix | Delete
@logdev.close if @logdev
[416] Fix | Delete
end
[417] Fix | Delete
[418] Fix | Delete
private
[419] Fix | Delete
[420] Fix | Delete
# Severity label for logging. (max 5 char)
[421] Fix | Delete
SEV_LABEL = %w(DEBUG INFO WARN ERROR FATAL ANY)
[422] Fix | Delete
[423] Fix | Delete
def format_severity(severity)
[424] Fix | Delete
SEV_LABEL[severity] || 'ANY'
[425] Fix | Delete
end
[426] Fix | Delete
[427] Fix | Delete
def format_message(severity, datetime, progname, msg)
[428] Fix | Delete
(@formatter || @default_formatter).call(severity, datetime, progname, msg)
[429] Fix | Delete
end
[430] Fix | Delete
[431] Fix | Delete
[432] Fix | Delete
class Formatter
[433] Fix | Delete
Format = "%s, [%s#%d] %5s -- %s: %s\n"
[434] Fix | Delete
[435] Fix | Delete
attr_accessor :datetime_format
[436] Fix | Delete
[437] Fix | Delete
def initialize
[438] Fix | Delete
@datetime_format = nil
[439] Fix | Delete
end
[440] Fix | Delete
[441] Fix | Delete
def call(severity, time, progname, msg)
[442] Fix | Delete
Format % [severity[0..0], format_datetime(time), $$, severity, progname,
[443] Fix | Delete
msg2str(msg)]
[444] Fix | Delete
end
[445] Fix | Delete
[446] Fix | Delete
private
[447] Fix | Delete
[448] Fix | Delete
def format_datetime(time)
[449] Fix | Delete
if @datetime_format.nil?
[450] Fix | Delete
time.strftime("%Y-%m-%dT%H:%M:%S.") << "%06d " % time.usec
[451] Fix | Delete
else
[452] Fix | Delete
time.strftime(@datetime_format)
[453] Fix | Delete
end
[454] Fix | Delete
end
[455] Fix | Delete
[456] Fix | Delete
def msg2str(msg)
[457] Fix | Delete
case msg
[458] Fix | Delete
when ::String
[459] Fix | Delete
msg
[460] Fix | Delete
when ::Exception
[461] Fix | Delete
"#{ msg.message } (#{ msg.class })\n" <<
[462] Fix | Delete
(msg.backtrace || []).join("\n")
[463] Fix | Delete
else
[464] Fix | Delete
msg.inspect
[465] Fix | Delete
end
[466] Fix | Delete
end
[467] Fix | Delete
end
[468] Fix | Delete
[469] Fix | Delete
[470] Fix | Delete
class LogDevice
[471] Fix | Delete
attr_reader :dev
[472] Fix | Delete
attr_reader :filename
[473] Fix | Delete
[474] Fix | Delete
class LogDeviceMutex
[475] Fix | Delete
include MonitorMixin
[476] Fix | Delete
end
[477] Fix | Delete
[478] Fix | Delete
def initialize(log = nil, opt = {})
[479] Fix | Delete
@dev = @filename = @shift_age = @shift_size = nil
[480] Fix | Delete
@mutex = LogDeviceMutex.new
[481] Fix | Delete
if log.respond_to?(:write) and log.respond_to?(:close)
[482] Fix | Delete
@dev = log
[483] Fix | Delete
else
[484] Fix | Delete
@dev = open_logfile(log)
[485] Fix | Delete
@dev.sync = true
[486] Fix | Delete
@filename = log
[487] Fix | Delete
@shift_age = opt[:shift_age] || 7
[488] Fix | Delete
@shift_size = opt[:shift_size] || 1048576
[489] Fix | Delete
end
[490] Fix | Delete
end
[491] Fix | Delete
[492] Fix | Delete
def write(message)
[493] Fix | Delete
@mutex.synchronize do
[494] Fix | Delete
if @shift_age and @dev.respond_to?(:stat)
[495] Fix | Delete
begin
[496] Fix | Delete
check_shift_log
[497] Fix | Delete
rescue
[498] Fix | Delete
raise Logger::ShiftingError.new("Shifting failed. #{$!}")
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function