防止Kivy离开调试消息

时间:2018-05-12 17:07:13

标签: python user-interface debugging terminal kivy

我有一个简单的Kivy接口也使用终端。

示例代码:

import kivy

kivy.require('1.0.6')

from kivy.app import App
from kivy.uix.label import Label

class MyApp(App):
    def build(self):
        return Label(text = 'Hello')

MyApp().run()

问题在于每当我启动脚本时,我都会得到这个:

[INFO   ] [Logger      ] Record log in C:\Users\Simon\.kivy\logs\kivy_18-05-12_37.txt
[INFO   ] [Kivy        ] v1.10.0
[INFO   ] [Python      ] v3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)]
[INFO   ] [Factory     ] 194 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [OSC         ] using <thread> for socket
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] GLEW initialization succeeded
[INFO   ] [GL          ] Backend used <glew>
[INFO   ] [GL          ] OpenGL version <b'4.5.13467 Compatibility Profile Context 21.19.414.1280'>
[INFO   ] [GL          ] OpenGL vendor <b'ATI Technologies Inc.'>
[INFO   ] [GL          ] OpenGL renderer <b'AMD Radeon R7 Graphics'>
[INFO   ] [GL          ] OpenGL parsed version: 4, 5
[INFO   ] [GL          ] Shading version <b'4.50'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <32>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [Base        ] Start application main loop
[INFO   ] [GL          ] NPOT texture support is available
[INFO   ] [Base        ] Leaving application in progress...

我发现这是Kivy的自动调试信息。如何防止它们(或至少隐藏以便我可以使用终端)?

1 个答案:

答案 0 :(得分:0)

现在您可以通过设置 Config 来设置您想要查看的日志类型。 您可以选择 tracedebuginfowarningerrorcritical

之一
from kivy.config import Config
# your code ...

if __name__ == '__main__':
    # path of the log directory
    Config.set('kivy', 'log_dir', 'your_chosen_log_dir_path')
    
    # filename of the log file
    Config.set('kivy', 'log_name', "anything_you_want_%y-%m-%d_%_.log")
    
    # Keep log_maxfiles recent logfiles while purging the log directory. Set ‘log_maxfiles’ to -1 to disable logfile purging (eg keep all logfiles).
    Config.set('kivy', 'log_maxfiles', 1000)

    # minimum log level which is what you need to not see kivy's default info logs
    Config.set('kivy', 'log_level', 'error')
    
    # apply all these changes
    Config.write()

请记住,设置配置变量并运行程序不会立即停止默认调试消息。您仍然会在第一次运行时看到它们。这是因为 Kivy 在第一次运行时设置了配置变量,然后在接下来的运行中应用它。因此,您需要运行两次才能看到效果。

设置 os.environ["KIVY_NO_CONSOLELOG"] = "1" 确实会删除调试消息,但它也不允许您查看错误和关键消息。所以如果你想看到错误和回溯,设置它不是一个明智的选择。

来源: