请求消息时找不到记录器“pubnub”错误的处理程序

2024-10-02 02:44:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在正在使用pubnub。我正在给我的pubnub帐户发送一些消息(状态:打开和状态:关)但是当我想在我的raspberry pi上运行python脚本时。我得到这个错误。在

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

我有最新的python版本 我有最新的公共版本

代码:

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()

Tags: tofromimporteventmessageledifdef
1条回答
网友
1楼 · 发布于 2024-10-02 02:44:47

我必须为pubnub logger初始化我自己的处理程序

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)

相关问题 更多 >

    热门问题