Python logging与Bokeh兼容性问题:Bokeh Server日志无法写入.log文件
Great question—Bokeh Server absolutely works with Python's logging module, so the issue is almost certainly in how you're configuring your logs, not a compatibility problem. Let's break down the most common reasons your .log file isn't being generated, and how to fix them:
1. Your logging configuration is running too early
If you're starting Bokeh Server via the bokeh serve command line tool, Bokeh initializes its own logging setup before your app code runs. That means any logging config you write at the top of your app script might get overwritten or ignored.
To fix this, use Bokeh's server lifecycle hooks to configure logging after the server has set up its loggers. For example, use the on_server_loaded hook:
from bokeh.io import curdoc import logging def setup_logging(server_context): # Get the Bokeh server logger (or use root logger if you want all logs) logger = logging.getLogger("bokeh.server") # Create a file handler file_handler = logging.FileHandler("bokeh_server.log") file_handler.setLevel(logging.INFO) # Add a formatter to make logs readable formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") file_handler.setFormatter(formatter) # Add the handler to the logger (this will redirect logs to your file) logger.addHandler(file_handler) # Set the log level to capture the messages you care about logger.setLevel(logging.INFO) # Register the hook to run when the server starts curdoc().on_server_loaded(setup_logging)
2. You're not targeting the right loggers
Bokeh uses specific logger names (like bokeh, bokeh.server, bokeh.server.connection) instead of just the root logger. If you only configure the root logger, you might miss Bokeh's internal logs.
If you want to capture both Bokeh's logs and your own custom logs, add your file handler to both the Bokeh logger and your app's logger:
# For your custom logs custom_logger = logging.getLogger(__name__) custom_logger.addHandler(file_handler) custom_logger.setLevel(logging.INFO) # Then in your app code: custom_logger.info("User performed an action!")
3. Permissions or file path issues
Double-check that your script has write permissions in the directory where you're trying to create the .log file. If you're specifying a relative path, make sure it's relative to where the Bokeh Server is running (not necessarily where your script is located). Try using an absolute path to rule this out:
file_handler = logging.FileHandler("/full/path/to/your/bokeh_server.log")
4. Log level is too high
If you set your logger to logging.ERROR but only emit INFO or DEBUG messages, nothing will get written to the file. Make sure your handler's log level matches or is lower than the messages you want to capture.
Example for script-based server
If you're starting the server directly from your Python script (not using bokeh serve), you can configure logging right before starting the server loop:
import logging from bokeh.server.server import Server from bokeh.plotting import figure, curdoc # Configure logging first logger = logging.getLogger("bokeh.server") file_handler = logging.FileHandler("bokeh_server.log") file_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")) logger.addHandler(file_handler) logger.setLevel(logging.INFO) # Your app logic def modify_doc(doc): p = figure() p.circle([1,2,3], [4,5,6]) doc.add_root(p) logging.info("Added plot to document") # Start server server = Server({"/": modify_doc}, port=5006) server.start() logging.info("Server started on port 5006") server.io_loop.start()
Give these fixes a try—your .log file should start capturing logs once you adjust the configuration to work with Bokeh's logging setup.
内容的提问来源于stack exchange,提问作者Gülsay

