Git Product home page Git Product logo

Comments (8)

mario-amazing avatar mario-amazing commented on June 2, 2024

I tried the same solution as in rails, but I don't see anything in the console:

        if ::Rails.logger.respond_to?(:broadcast_to)
          Rails.logger = ActiveSupport::BroadcastLogger.new(Rails.logger, console)
        else
          Rails.logger.extend ActiveSupport::Logger.broadcast console
        end

from telegram-bot.

epergo avatar epergo commented on June 2, 2024

UPDATE: I checked an old version because I didn't really remember the expected output and you are completely right, logs are missing.


Hey @mario-amazing I got the same error and applied the same patch as you (different condition but still) and it seems to be working fine

diff --git a/lib/tasks/telegram-bot.rake b/lib/tasks/telegram-bot.rake
index 01c5cd9..85371a6 100644
--- a/lib/tasks/telegram-bot.rake
+++ b/lib/tasks/telegram-bot.rake
@@ -7,7 +7,11 @@ namespace :telegram do
       Rake::Task['environment'].invoke
       if ENV.fetch('LOG_TO_STDOUT') { Rails.env.development? }.present?
         console = ActiveSupport::Logger.new(STDERR)
-        Rails.logger.extend ActiveSupport::Logger.broadcast console
+        if Gem::Version.new(Rails.version) >= Gem::Version.new('7.1')
+          Rails.logger = ActiveSupport::BroadcastLogger.new(Rails.logger, console)
+        else
+          Rails.logger.extend ActiveSupport::Logger.broadcast console
+        end
       end
       Telegram::Bot::UpdatesPoller.start(ENV['BOT'].try!(:to_sym) || :default)
     end
class TelegramController < Telegram::Bot::UpdatesController
  # ... Unrelated code

  def message(message)
    Rails.logger.info("Received a message!!")
  end
bin/rails telegram:bot:poller
Received a message!!

from telegram-bot.

epergo avatar epergo commented on June 2, 2024

So the thing is that when invoking the environment task we are already creating instances of Telegram::Bot::UpdatesPoller and those instances memoize the initial logger.

Once we overwrite the logger Rails.logger = ActiveSupport::BroadcastLogger.new(console) new instances will have the correct one but the others won't.

If we update the Rails logger before invoking the environment task, all instances of the poller will have the correct logger.

diff --git a/lib/tasks/telegram-bot.rake b/lib/tasks/telegram-bot.rake
index 01c5cd9..3b5c433 100644
--- a/lib/tasks/telegram-bot.rake
+++ b/lib/tasks/telegram-bot.rake
@@ -4,10 +4,15 @@ namespace :telegram do
       'Use LOG_TO_STDOUT to enable/disable broadcasting.'
     task :poller do
       ENV['BOT_POLLER_MODE'] = 'true'
-      Rake::Task['environment'].invoke
       if ENV.fetch('LOG_TO_STDOUT') { Rails.env.development? }.present?
         console = ActiveSupport::Logger.new(STDERR)
-        Rails.logger.extend ActiveSupport::Logger.broadcast console
+        if Gem::Version.new(Rails.version) >= Gem::Version.new('7.1')
+          Rails.logger = ActiveSupport::BroadcastLogger.new(console)
+          Rake::Task['environment'].invoke
+        else
+          Rake::Task['environment'].invoke
+          Rails.logger.extend ActiveSupport::Logger.broadcast console
+        end
       end
       Telegram::Bot::UpdatesPoller.start(ENV['BOT'].try!(:to_sym) || :default)
     end

Don't know if this is the best solution though, @mario-amazing could you do some testing in your project to confirm this works?

from telegram-bot.

mario-amazing avatar mario-amazing commented on June 2, 2024

@epergo Results:
Rails 7.1 logs with /start command:
image
Rails 7.0
Screenshot 2023-11-12 at 18 58 01

Rails.logger.level
=> 0

from telegram-bot.

epergo avatar epergo commented on June 2, 2024

Did you first apply the patch in my second message?

from telegram-bot.

mario-amazing avatar mario-amazing commented on June 2, 2024

ofc, without this patch I can't even start a poll

from telegram-bot.

galievruslan avatar galievruslan commented on June 2, 2024

Hello. I have the same issue.
I had to make a rake task to run on dev:

namespace :telegram do
   desc "Start Telegram bot"
   task :poller do
     ENV['BOT_POLLER_MODE'] = 'true'
     if ENV.fetch('LOG_TO_STDOUT') { Rails.env.development? }.present?
       console = ActiveSupport::Logger.new(STDERR)
       if Gem::Version.new(Rails.version) >= Gem::Version.new('7.1')
         Rails.logger = ActiveSupport::BroadcastLogger.new(console)
         Rake::Task['environment'].invoke
       else
         Rake::Task['environment'].invoke
         Rails.logger.extend ActiveSupport::Logger.broadcast console
       end
     end
     Telegram::Bot::UpdatesPoller.start(ENV['BOT'].try!(:to_sym) || :default)
   end
end

It works when I run it through
bundle exec rake telegram:poller
but it doesn't work when I run it through Foreman.
Procfile.dev:

web: env RUBY_DEBUG_OPEN=true && unset PORT && puma -C config/puma.rb
log: tail -f log/development.log
js: yarn build --watch
css: yarn build:css --watch
sidekiq: bundle exec sidekiq -C config/sidekiq.yml
telegram: bundle exec rake telegram: poller

foreman start -f Procfile.dev

12:06:22 telegram.1 | rake aborted!
12:06:22 telegram.1 | Telegram::Bot::NotFound: Not Found (Telegram::Bot::NotFound)
12:06:22 telegram.1 | /home/ruslan/.rvm/gems/ruby-3.2.2/gems/telegram-bot-0.16.0/lib/telegram/bot/client.rb:67:in `request'
12:06:22 telegram.1 | /home/ruslan/.rvm/gems/ruby-3.2.2/gems/telegram-bot-0.16.0/lib/telegram/bot/async.rb:151:in `request'
12:06:22 telegram.1 | /home/ruslan/.rvm/gems/ruby-3.2.2/gems/telegram-bot-0.16.0/lib/telegram/bot/client/api_helper.rb:22:in `block (2 levels) in define_helpers'
12:06:22 telegram.1 | /home/ruslan/.rvm/gems/ruby-3.2.2/gems/telegram-bot-0.16.0/lib/telegram/bot/updates_poller.rb:71:in `block in fetch_updates'
12:06:22 telegram.1 | /home/ruslan/.rvm/gems/ruby-3.2.2/gems/telegram-bot-0.16.0/lib/telegram/bot/async.rb:138:in `async'
12:06:22 telegram.1 | /home/ruslan/.rvm/gems/ruby-3.2.2/gems/telegram-bot-0.16.0/lib/telegram/bot/updates_poller.rb:71:in `fetch_updates'
12:06:22 telegram.1 | /home/ruslan/.rvm/gems/ruby-3.2.2/gems/telegram-bot-0.16.0/lib/telegram/bot/updates_poller.rb:58:in `run'
12:06:22 telegram.1 | /home/ruslan/.rvm/gems/ruby-3.2.2/gems/telegram-bot-0.16.0/lib/telegram/bot/updates_poller.rb:47:in `start'
12:06:22 telegram.1 | /home/ruslan/.rvm/gems/ruby-3.2.2/gems/telegram-bot-0.16.0/lib/telegram/bot/updates_poller.rb:21:in `start'
12:06:22 telegram.1 | /media/ruslan/team/charity_crew/lib/tasks/telegram-bot.rake:15:in `block (2 levels) in <main>'
12:06:22 telegram.1 | /home/ruslan/.rvm/gems/ruby-3.2.2/gems/rake-13.1.0/exe/rake:27:in `<top (required)>'
12:06:22 telegram.1 | /home/ruslan/.rvm/gems/ruby-3.2.2/bin/ruby_executable_hooks:22:in `eval'
12:06:22 telegram.1 | /home/ruslan/.rvm/gems/ruby-3.2.2/bin/ruby_executable_hooks:22:in `<main>'
12:06:22 telegram.1 | Tasks: TOP => telegram:poller

from telegram-bot.

mario-amazing avatar mario-amazing commented on June 2, 2024

Found solution!

        if ::Rails.logger.respond_to?(:broadcast_to)
          ::Rails.logger.broadcast_to(console)
        else
          Rails.logger.extend ActiveSupport::Logger.broadcast console
        end

from telegram-bot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.