Write Errors to Log File in Ruby
up vote
-3
down vote
favorite
I am trying to capture errors, check for a /tmp directory and then write the error to a logfile in that directory, currently I get:
.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/fileutils.rb:252:in `mkdir': Permission denied @ dir_s_mkdir - /temp
Here is my code:
require 'logger'
require 'tmpdir'
temp = Dir.tmpdir()
log = Logger.new File.open("#{temp}/error.log", 'w')
log.level = Logger::INFO
begin
rescue StandardError => e
log.error "Error - #{e}"
puts "For detailed error messages, see: #{temp}/error.log"
end
I believe this error is because I am attempting to do something I don't have permission to do, what I don't understand is there a clean way to achieve what I am attempting? Thanks in advance for any time spent on this issue.
I have edited this with my updated code that answers my question. Thanks for all your input.
ruby logging error-handling fileutils
add a comment |
up vote
-3
down vote
favorite
I am trying to capture errors, check for a /tmp directory and then write the error to a logfile in that directory, currently I get:
.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/fileutils.rb:252:in `mkdir': Permission denied @ dir_s_mkdir - /temp
Here is my code:
require 'logger'
require 'tmpdir'
temp = Dir.tmpdir()
log = Logger.new File.open("#{temp}/error.log", 'w')
log.level = Logger::INFO
begin
rescue StandardError => e
log.error "Error - #{e}"
puts "For detailed error messages, see: #{temp}/error.log"
end
I believe this error is because I am attempting to do something I don't have permission to do, what I don't understand is there a clean way to achieve what I am attempting? Thanks in advance for any time spent on this issue.
I have edited this with my updated code that answers my question. Thanks for all your input.
ruby logging error-handling fileutils
2
You are lacking privileges. Why try to write in/temp
, why not just/tmp
? Create a directory somewhere where the user running the script has more privileges, not in root, or use/tmp
. Or run your script withsudo
.
– Marcin Kołodziej
Nov 19 at 5:25
1
Don't bother specifying the path for a temp directory. Just useDir.tmpdir
and/orDir.mktmpdir
and let Ruby do the lifting for you: ruby-doc.org/stdlib-2.5.3/libdoc/tmpdir/rdoc/Dir.html
– anothermh
Nov 19 at 5:56
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
I am trying to capture errors, check for a /tmp directory and then write the error to a logfile in that directory, currently I get:
.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/fileutils.rb:252:in `mkdir': Permission denied @ dir_s_mkdir - /temp
Here is my code:
require 'logger'
require 'tmpdir'
temp = Dir.tmpdir()
log = Logger.new File.open("#{temp}/error.log", 'w')
log.level = Logger::INFO
begin
rescue StandardError => e
log.error "Error - #{e}"
puts "For detailed error messages, see: #{temp}/error.log"
end
I believe this error is because I am attempting to do something I don't have permission to do, what I don't understand is there a clean way to achieve what I am attempting? Thanks in advance for any time spent on this issue.
I have edited this with my updated code that answers my question. Thanks for all your input.
ruby logging error-handling fileutils
I am trying to capture errors, check for a /tmp directory and then write the error to a logfile in that directory, currently I get:
.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/fileutils.rb:252:in `mkdir': Permission denied @ dir_s_mkdir - /temp
Here is my code:
require 'logger'
require 'tmpdir'
temp = Dir.tmpdir()
log = Logger.new File.open("#{temp}/error.log", 'w')
log.level = Logger::INFO
begin
rescue StandardError => e
log.error "Error - #{e}"
puts "For detailed error messages, see: #{temp}/error.log"
end
I believe this error is because I am attempting to do something I don't have permission to do, what I don't understand is there a clean way to achieve what I am attempting? Thanks in advance for any time spent on this issue.
I have edited this with my updated code that answers my question. Thanks for all your input.
ruby logging error-handling fileutils
ruby logging error-handling fileutils
edited 2 days ago
asked Nov 19 at 4:15
mrtriangle
300723
300723
2
You are lacking privileges. Why try to write in/temp
, why not just/tmp
? Create a directory somewhere where the user running the script has more privileges, not in root, or use/tmp
. Or run your script withsudo
.
– Marcin Kołodziej
Nov 19 at 5:25
1
Don't bother specifying the path for a temp directory. Just useDir.tmpdir
and/orDir.mktmpdir
and let Ruby do the lifting for you: ruby-doc.org/stdlib-2.5.3/libdoc/tmpdir/rdoc/Dir.html
– anothermh
Nov 19 at 5:56
add a comment |
2
You are lacking privileges. Why try to write in/temp
, why not just/tmp
? Create a directory somewhere where the user running the script has more privileges, not in root, or use/tmp
. Or run your script withsudo
.
– Marcin Kołodziej
Nov 19 at 5:25
1
Don't bother specifying the path for a temp directory. Just useDir.tmpdir
and/orDir.mktmpdir
and let Ruby do the lifting for you: ruby-doc.org/stdlib-2.5.3/libdoc/tmpdir/rdoc/Dir.html
– anothermh
Nov 19 at 5:56
2
2
You are lacking privileges. Why try to write in
/temp
, why not just /tmp
? Create a directory somewhere where the user running the script has more privileges, not in root, or use /tmp
. Or run your script with sudo
.– Marcin Kołodziej
Nov 19 at 5:25
You are lacking privileges. Why try to write in
/temp
, why not just /tmp
? Create a directory somewhere where the user running the script has more privileges, not in root, or use /tmp
. Or run your script with sudo
.– Marcin Kołodziej
Nov 19 at 5:25
1
1
Don't bother specifying the path for a temp directory. Just use
Dir.tmpdir
and/or Dir.mktmpdir
and let Ruby do the lifting for you: ruby-doc.org/stdlib-2.5.3/libdoc/tmpdir/rdoc/Dir.html– anothermh
Nov 19 at 5:56
Don't bother specifying the path for a temp directory. Just use
Dir.tmpdir
and/or Dir.mktmpdir
and let Ruby do the lifting for you: ruby-doc.org/stdlib-2.5.3/libdoc/tmpdir/rdoc/Dir.html– anothermh
Nov 19 at 5:56
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
To make it work and compatible with Windows:
require 'logger'
require 'tmpdir'
tmp = Dir.mktmpdir
log = Logger.new File.open(File.join(tmp, 'error.log', 'w')
log.level = Logger::INFO
begin
# your code here
rescue StandardError => e
log.error "Error - #{e}"
puts "For detailed error messages, see the file: /temp/error.log"
end
add a comment |
up vote
-1
down vote
- You shouldn't create /tmp directory - this directory always exist.
- You should place
begin
above checking code, not below.
require 'logger'
require 'fileutils'
begin
log = Logger.new File.open('/tmp/error.log', 'w')
log.level = Logger::INFO
rescue StandardError => e
puts "Error - #{e}"
end
What about on a windows system? I want to check if a tmp directory exists and if not, create it and write the log file there.
– mrtriangle
Nov 19 at 5:37
1
It's/tmp
, not/temp
that should exist. Rescuing from opening a file which is supposed to be used for error logging and logging that error to the same file does not make sense.
– Marcin Kołodziej
Nov 19 at 5:37
2
@mrtriangle if you're trying to make your program compatible with both Windows and Unix, create your logs somewhere inside your application directory (or handle that in an OS-specific way).
– Marcin Kołodziej
Nov 19 at 5:38
1
@mrtriangle, on most Windows system you can't create directory at root level without special permissions.
– Ivan Olshansky
Nov 19 at 5:41
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
To make it work and compatible with Windows:
require 'logger'
require 'tmpdir'
tmp = Dir.mktmpdir
log = Logger.new File.open(File.join(tmp, 'error.log', 'w')
log.level = Logger::INFO
begin
# your code here
rescue StandardError => e
log.error "Error - #{e}"
puts "For detailed error messages, see the file: /temp/error.log"
end
add a comment |
up vote
1
down vote
To make it work and compatible with Windows:
require 'logger'
require 'tmpdir'
tmp = Dir.mktmpdir
log = Logger.new File.open(File.join(tmp, 'error.log', 'w')
log.level = Logger::INFO
begin
# your code here
rescue StandardError => e
log.error "Error - #{e}"
puts "For detailed error messages, see the file: /temp/error.log"
end
add a comment |
up vote
1
down vote
up vote
1
down vote
To make it work and compatible with Windows:
require 'logger'
require 'tmpdir'
tmp = Dir.mktmpdir
log = Logger.new File.open(File.join(tmp, 'error.log', 'w')
log.level = Logger::INFO
begin
# your code here
rescue StandardError => e
log.error "Error - #{e}"
puts "For detailed error messages, see the file: /temp/error.log"
end
To make it work and compatible with Windows:
require 'logger'
require 'tmpdir'
tmp = Dir.mktmpdir
log = Logger.new File.open(File.join(tmp, 'error.log', 'w')
log.level = Logger::INFO
begin
# your code here
rescue StandardError => e
log.error "Error - #{e}"
puts "For detailed error messages, see the file: /temp/error.log"
end
answered 2 days ago
Dorian
12.5k37383
12.5k37383
add a comment |
add a comment |
up vote
-1
down vote
- You shouldn't create /tmp directory - this directory always exist.
- You should place
begin
above checking code, not below.
require 'logger'
require 'fileutils'
begin
log = Logger.new File.open('/tmp/error.log', 'w')
log.level = Logger::INFO
rescue StandardError => e
puts "Error - #{e}"
end
What about on a windows system? I want to check if a tmp directory exists and if not, create it and write the log file there.
– mrtriangle
Nov 19 at 5:37
1
It's/tmp
, not/temp
that should exist. Rescuing from opening a file which is supposed to be used for error logging and logging that error to the same file does not make sense.
– Marcin Kołodziej
Nov 19 at 5:37
2
@mrtriangle if you're trying to make your program compatible with both Windows and Unix, create your logs somewhere inside your application directory (or handle that in an OS-specific way).
– Marcin Kołodziej
Nov 19 at 5:38
1
@mrtriangle, on most Windows system you can't create directory at root level without special permissions.
– Ivan Olshansky
Nov 19 at 5:41
add a comment |
up vote
-1
down vote
- You shouldn't create /tmp directory - this directory always exist.
- You should place
begin
above checking code, not below.
require 'logger'
require 'fileutils'
begin
log = Logger.new File.open('/tmp/error.log', 'w')
log.level = Logger::INFO
rescue StandardError => e
puts "Error - #{e}"
end
What about on a windows system? I want to check if a tmp directory exists and if not, create it and write the log file there.
– mrtriangle
Nov 19 at 5:37
1
It's/tmp
, not/temp
that should exist. Rescuing from opening a file which is supposed to be used for error logging and logging that error to the same file does not make sense.
– Marcin Kołodziej
Nov 19 at 5:37
2
@mrtriangle if you're trying to make your program compatible with both Windows and Unix, create your logs somewhere inside your application directory (or handle that in an OS-specific way).
– Marcin Kołodziej
Nov 19 at 5:38
1
@mrtriangle, on most Windows system you can't create directory at root level without special permissions.
– Ivan Olshansky
Nov 19 at 5:41
add a comment |
up vote
-1
down vote
up vote
-1
down vote
- You shouldn't create /tmp directory - this directory always exist.
- You should place
begin
above checking code, not below.
require 'logger'
require 'fileutils'
begin
log = Logger.new File.open('/tmp/error.log', 'w')
log.level = Logger::INFO
rescue StandardError => e
puts "Error - #{e}"
end
- You shouldn't create /tmp directory - this directory always exist.
- You should place
begin
above checking code, not below.
require 'logger'
require 'fileutils'
begin
log = Logger.new File.open('/tmp/error.log', 'w')
log.level = Logger::INFO
rescue StandardError => e
puts "Error - #{e}"
end
edited Nov 19 at 5:44
answered Nov 19 at 5:33
Ivan Olshansky
23217
23217
What about on a windows system? I want to check if a tmp directory exists and if not, create it and write the log file there.
– mrtriangle
Nov 19 at 5:37
1
It's/tmp
, not/temp
that should exist. Rescuing from opening a file which is supposed to be used for error logging and logging that error to the same file does not make sense.
– Marcin Kołodziej
Nov 19 at 5:37
2
@mrtriangle if you're trying to make your program compatible with both Windows and Unix, create your logs somewhere inside your application directory (or handle that in an OS-specific way).
– Marcin Kołodziej
Nov 19 at 5:38
1
@mrtriangle, on most Windows system you can't create directory at root level without special permissions.
– Ivan Olshansky
Nov 19 at 5:41
add a comment |
What about on a windows system? I want to check if a tmp directory exists and if not, create it and write the log file there.
– mrtriangle
Nov 19 at 5:37
1
It's/tmp
, not/temp
that should exist. Rescuing from opening a file which is supposed to be used for error logging and logging that error to the same file does not make sense.
– Marcin Kołodziej
Nov 19 at 5:37
2
@mrtriangle if you're trying to make your program compatible with both Windows and Unix, create your logs somewhere inside your application directory (or handle that in an OS-specific way).
– Marcin Kołodziej
Nov 19 at 5:38
1
@mrtriangle, on most Windows system you can't create directory at root level without special permissions.
– Ivan Olshansky
Nov 19 at 5:41
What about on a windows system? I want to check if a tmp directory exists and if not, create it and write the log file there.
– mrtriangle
Nov 19 at 5:37
What about on a windows system? I want to check if a tmp directory exists and if not, create it and write the log file there.
– mrtriangle
Nov 19 at 5:37
1
1
It's
/tmp
, not /temp
that should exist. Rescuing from opening a file which is supposed to be used for error logging and logging that error to the same file does not make sense.– Marcin Kołodziej
Nov 19 at 5:37
It's
/tmp
, not /temp
that should exist. Rescuing from opening a file which is supposed to be used for error logging and logging that error to the same file does not make sense.– Marcin Kołodziej
Nov 19 at 5:37
2
2
@mrtriangle if you're trying to make your program compatible with both Windows and Unix, create your logs somewhere inside your application directory (or handle that in an OS-specific way).
– Marcin Kołodziej
Nov 19 at 5:38
@mrtriangle if you're trying to make your program compatible with both Windows and Unix, create your logs somewhere inside your application directory (or handle that in an OS-specific way).
– Marcin Kołodziej
Nov 19 at 5:38
1
1
@mrtriangle, on most Windows system you can't create directory at root level without special permissions.
– Ivan Olshansky
Nov 19 at 5:41
@mrtriangle, on most Windows system you can't create directory at root level without special permissions.
– Ivan Olshansky
Nov 19 at 5:41
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53368221%2fwrite-errors-to-log-file-in-ruby%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You are lacking privileges. Why try to write in
/temp
, why not just/tmp
? Create a directory somewhere where the user running the script has more privileges, not in root, or use/tmp
. Or run your script withsudo
.– Marcin Kołodziej
Nov 19 at 5:25
1
Don't bother specifying the path for a temp directory. Just use
Dir.tmpdir
and/orDir.mktmpdir
and let Ruby do the lifting for you: ruby-doc.org/stdlib-2.5.3/libdoc/tmpdir/rdoc/Dir.html– anothermh
Nov 19 at 5:56