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.










share|improve this question




















  • 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






  • 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















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.










share|improve this question




















  • 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






  • 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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 with sudo.
    – Marcin Kołodziej
    Nov 19 at 5:25






  • 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














  • 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






  • 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








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












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





share|improve this answer




























    up vote
    -1
    down vote














    1. You shouldn't create /tmp directory - this directory always exist.

    2. 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





    share|improve this answer























    • 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













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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





    share|improve this answer

























      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





      share|improve this answer























        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





        share|improve this answer












        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago









        Dorian

        12.5k37383




        12.5k37383
























            up vote
            -1
            down vote














            1. You shouldn't create /tmp directory - this directory always exist.

            2. 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





            share|improve this answer























            • 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

















            up vote
            -1
            down vote














            1. You shouldn't create /tmp directory - this directory always exist.

            2. 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





            share|improve this answer























            • 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















            up vote
            -1
            down vote










            up vote
            -1
            down vote










            1. You shouldn't create /tmp directory - this directory always exist.

            2. 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





            share|improve this answer















            1. You shouldn't create /tmp directory - this directory always exist.

            2. 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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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




















            • 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




















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]