Class: Log
- Inherits:
-
Object
- Object
- Log
- Defined in:
- lib/log.rb
Overview
Class to do automated logging to the console, to a logfile and to an email-account.
It is possible to set the verbosity of logging and various other options.
$Id$
Instance Method Summary collapse
-
#initialize(logfile = "", advancedlog = true, syslog = false) ⇒ Log
constructor
Initializes the logger object.
-
#initialize_email(email, smtp, port, uname, passwd, tls, testm, backupname) ⇒ Object
Initialize the email function, and check if all the given parameters are correct.
-
#p(message, level = 1, date = false) ⇒ Object
levels are: * 0: debug * 1: info * 2: notice * 3: warning * 4: error * 98: print only to command line (special case) * 99: successmail (special case).
-
#sendmail(message) ⇒ Object
Sends an email with the appropriate message.
Constructor Details
#initialize(logfile = "", advancedlog = true, syslog = false) ⇒ Log
Initializes the logger object. Takes an (optional) path as argument, and a string (“yes” or “no”) or boolean value for the verbosity level within this logfile.
If the logfile is not yet existing, a new one will be created, if possible. Otherwise an error is thrown.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/log.rb', line 16 def initialize(logfile="", advancedlog=true, syslog=false) @logfile=logfile.strip @syslog = syslog @mail_initialized = false @smpt=false @logging_to_file = false unless @logfile == "" @logfile.insert(0, '/') unless @logfile[0, 1].to_s == "/" @logging_to_file = true end @logging_to_file = false if @syslog @slog = SyslogWrapper.new() if @syslog @advancedlog=advancedlog if @logging_to_file and not Directory.writable?(@logfile) @initialized = false raise @logfile.to_s + " is not writable!" elsif @logging_to_file and not File.file?(@logfile) array = @logfile.split("/") array.delete_at(-1) FileUtils.makedirs("/" + array.join("/")) end @initialized = true end |
Instance Method Details
#initialize_email(email, smtp, port, uname, passwd, tls, testm, backupname) ⇒ Object
Initialize the email function, and check if all the given parameters are correct. If tls is true, then see if ruby-openssl is installed.
If any errors occur, it raises an error.
Also sends a test email, if testm is set.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/log.rb', line 52 def initialize_email(email, smtp, port, uname, passwd, tls, testm, backupname) @email=email @smtp=smtp @port=port @uname=uname @passwd=passwd @tls=tls @testm=testm @backupname = backupname if (@uname == "" and @passwd != "") or (@passwd == "" and @uname != "") @mail_initialized=false raise "Both or none of the uname and the passwd must be specified!" end if @port == "" @mail_initialized = false raise "Port mustn't be empty to send e-mails!" end if @smpt == "" @mail_initialized = false raise "Smtp mustn't be empty to send e-mails!" end unless (@email =~ /./ and @email =~ /@/) @mail_initialized = false raise "E-mail provided in Configfile is wrong or empty!" end @mail_initialized=true if @testm text='From: smarbs <' + @email + '> To: smarbs Admin <'+@email+'> Subject: Test email, ' + @backupname + ' This test email was sent to check the smtp-connection of the configfile "' + @backupname + '". The "testm" option in the script should have been automatically disabled by now, so you wont receive test emails anymore. --- For any questions concerning smarbs, you can contact me here: [email protected]' sendmail(text) end end |
#p(message, level = 1, date = false) ⇒ Object
levels are:
-
0: debug
-
1: info
-
2: notice
-
3: warning
-
4: error
-
98: print only to command line (special case)
-
99: successmail (special case)
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/log.rb', line 117 def p(, level=1, date=false) case level when 0..1 puts @slog.log(, level) if @syslog =Time.now.strftime("%d %b %Y %Hh%M: ")+ if date File.open(@logfile, "a") {|f| f.puts } if @logging_to_file and @advancedlog when 2..3 puts @slog.log(, level) if @syslog =Time.now.strftime("%d %b %Y %Hh%M: ")+ if date File.open(@logfile, "a") {|f| f.puts } if @logging_to_file when 4 @slog.log(, level) if @syslog = Time.now.strftime("\n%d %b %Y %Hh%M: Backup NOT successful!\n") + "The following error occured: " + puts File.open(@logfile, "a") {|f| f.puts } if @logging_to_file if @mail_initialized text='From: smarbs <' + @email + '> To: smarbs Admin <'+@email+'> Subject: An error occured in ' + @backupname + ' An error occured in ' + @backupname + ":\n" + if @logging_to_file text += ' Here is the full logfile: ' + IO.read(@logfile) end text +=' --- For any questions concerning smarbs, you can contact me here: [email protected]' sendmail(text) end when 98 puts when 99 if @mail_initialized then text='From: smarbs <' + @email + '> To: smarbs Admin <'+@email+'> Subject: Smarbs, backup "' + @backupname + '" successful Smarbs backup ended with the following message: ' + if @logging_to_file then text += ' Here is the full logfile: ' + IO.read(@logfile) end text += ' --- For any questions concerning smarbs, you can contact me here: [email protected]' sendmail(text) end end end |
#sendmail(message) ⇒ Object
Sends an email with the appropriate message.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/log.rb', line 185 def sendmail() begin smtp = Net::SMTP.new(@smtp, @port) if @uname != "" if @tls then smtp.enable_starttls smtp.start("localhost.localdomain",@uname,@passwd, :plain) do |i| i. , @email, @email end else smtp.start("localhost.localdomain",@uname,@passwd, :login) do |i| i. , @email, @email end end else smtp.start do |i| i. , @email, @email end end rescue Timeout::Error @mail_initialized=false raise "Error, SMTP timed out, " + $!.to_s rescue @mail_initialized=false raise "Error with SMTP authentification, " + $!.to_s end end |