Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8320881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:49:09+00:00 2026-06-08T22:49:09+00:00

I have run into a problem I have beat my head against the wall.

  • 0

I have run into a problem I have beat my head against the wall. Basically, I’m using this logging setup to log to a file, and it works great! However, I want to use this inside a method I built.. it won’t let me! It says it’s not a defined variable. I’m guessing this is a problem with variable scoping. How can I fix this?

This is my logging setup:

class MultiDelegator
  def initialize(*targets)
    @targets = targets
  end

  def self.delegate(*methods)
    methods.each do |m|
      define_method(m) do |*args|
        @targets.map { |t| t.send(m, *args) }
      end
    end
    self
  end

  class <<self
    alias to new
  end
end

log_file = File.open("/opt/sysnovo/log/server.log", "a")
log = Logger.new MultiDelegator.delegate(:write, :close).to(STDOUT, log_file

This is my method (deletes files):

def cleanup
    log.info "Running cleanup"
    dirs = [ "/opt/sysnovo/tmp", "/opt/sysnovo/data", "/opt/sysnovo/merge" ]
    dirs.each do |dir|
        # Our file count.
        file_count = Dir.glob(File.join("#{dir}", '**', '*')).select { |file| File.file?(file) }.count

        # Log and delete!
        log.info "Removing #{file_count} files in #{dir}"
        FileUtils.rm_rf("#{dir}/.", secure: true)
    end
end

So basically, the log.info INSIDE the method fails with this:

./sysnovo_server.rb:58:in `<main>': undefined local variable or method `log' for main:Object (NameError)

So my QUESTION is this: how can I set up this to work right? Or get the log.info to work inside my method. Or .. am I doing this wrong? I’m new to Ruby so be gentle.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:49:13+00:00Added an answer on June 8, 2026 at 10:49 pm

    If you want to use the log variable inside your method then you should define it as global variable like this

    $log = Logger.new MultiDelegator.delegate(:write, :close).to(STDOUT, log_file)
    

    But I would recommend that you create a Singleton class which handles the logging and use it to log.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SWFUpload to handle file uploads and have run into a problem where
I have run into this problem a few times and I'm not happy with
I have run into this problem again and now really need to know how
Apparently many people have run into this problem, but I have yet to be
I have run into this problem a lot in the past, but never really
I have run into this problem: Custom Methods & Extension Methods cannot be translated
I have run into this problem across multiple programming languages and I was just
This question is quite simple but I have run into the problem a few
I have run into this problem multiple times, and I cannot figure out why
I have run into this problem a few times and have never come up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.