【问题标题】:No handlers could be found for logger "pubnub" Error when asking for message请求消息时找不到记录器“pubnub”错误的处理程序
【发布时间】:2017-12-16 02:48:21
【问题描述】:

我目前正在使用 pubnub。我正在向我的 pubnub 帐户(状态:打开和状态:关闭)发送一些消息,但是当我想在我的树莓派上运行我的 python 脚本时。我得到这个错误。

找不到记录器“pubnub”的处理程序

我有最新的python版本 我有最新的 pubnub 版本

代码:

from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.callbacks import SubscribeCallback
from pubnub.enums import PNOperationType, PNStatusCategory
from pubnub.pubnub import PubNub, SubscribeListener
import time
import RPi.GPIO as GPIO

pnconf = PNConfiguration()
pnconf.subscribe_key = "sub-c-e1af6266-e1d0-11e7-ad36-deb77ae39924"
pnconf.publish_key = "pub-c-6dbd1d39-2915-4df0-b99a-5b358cf69209"
pnconf.ssl = True
pubnub = PubNub(pnconf)

def my_publish_callback(envelope, status):
    # Check whether request successfully completed or not
    if not status.is_error():
        pass  # Message successfully published to specified channel.
    else:
        pass  # Handle message publish error. Check 'category' property to find out possible issue
        # because of which request did fail.
        # Request can be resent using: [status retry];

class MySubscribeCallback(SubscribeCallback):
    def presence(self, pubnub, presence):
        pass  # handle incoming presence data

    def status(self, pubnub, status):
        if status.category == PNStatusCategory.PNUnexpectedDisconnectCategory:
            pass  # This event happens when radio / connectivity is lost

        elif status.category == PNStatusCategory.PNConnectedCategory:
            # Connect event. You can do stuff like publish, and know you'll get it.
            # Or just use the connected event to confirm you are subscribed for
            # UI / internal notifications, etc
            pubnub.publish().channel("led").message("hello!!").async(my_publish_callback)
        elif status.category == PNStatusCategory.PNReconnectedCategory:
            pass
            # Happens as part of our regular operation. This event happens when
            # radio / connectivity is lost, then regained.
        elif status.category == PNStatusCategory.PNDecryptionErrorCategory:
            pass
            # Handle message decryption error. Probably client configured to
            # encrypt messages and on live data feed it received plain text.

    def message(self, pubnub, message):
        print(message.message)
        if 'status' in message.message:
            print(message.message['status'])
            whatToDo(message.message['status'])
        pass  # Handle new message stored in message.message

def whatToDo(status):
    if status == 'ON':
        print('Switch ON light')
        # switch on
        led.on()
    else:
        print('Swtich OFF light')
        # switch off
        led.off()

pubnub.add_listener(MySubscribeCallback())
pubnub.subscribe().channels('led').execute()

【问题讨论】:

  • 是错误还是警告
  • 这通常只是为了让您知道提到的模块/包正在尝试使用内置的 logging 模块记录一些消息,并且您没有配置任何东西来查看/记录这些消息.它不应阻止您的代码正常工作。如果您想在屏幕上查看日志消息:import logging; logging.basicConfig(level=logging.INFO) 或其他组合。研究 python 的内置日志记录。
  • @daveydave400 感谢您的回复!我真的不知道..当我运行我的 python 代码时,它应该给出我通过 pubnub 发送的值,但它只是停止程序并向我显示那句话。
  • 嘿,你有没有想过这个问题?

标签: python raspberry-pi pubnub


【解决方案1】:

我必须为 pubnub 记录器初始化我自己的处理程序

import logging

# Avoid logger Errors and register your own logger

# create logger with pubnub
logger = logging.getLogger('pubnub')

logger.setLevel(logging.DEBUG)  # set to other levels in different environments ie (test / prod)

# create file handler which logs even debug messages
fh = logging.FileHandler('pubnub.log')
fh.setLevel(logging.DEBUG)
# register your handler to pubnub log
logger.addHandler(fh)

【讨论】:

    猜你喜欢
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 2017-10-26
    • 2012-11-11
    • 2011-09-03
    相关资源
    最近更新 更多