How to Log SQL Queries in Webman PHP Microframework with the Official Log Plugin
This guide explains how to install Webman's official log plugin and ThinkORM, configure database settings, execute SQL queries, and locate the generated SQL logs, while also covering important configuration tweaks to ensure proper logging behavior.
Overview
Webman is a modern PHP micro‑framework that supports middleware and components. Recording SQL statements to logs is useful for debugging and monitoring database interactions.
Installing the Log Plugin
Run the following command to add the official log plugin: composer require webman/log The database plugin relies on ThinkORM, which can be installed with: composer require -W webman/think-orm After installation a configuration file config/thinkorm.php is generated; edit it with the actual database credentials.
Executing an SQL Query
Example query using ThinkORM:
think\facade\Db::table('config_info')->where('id',1)->select();After installing, restart Webman.
Log Location
SQL logs are written under runtime/logs. A sample log entry shows request information, connection details, and the executed SQL with runtime metrics:
[2024-07-05 17:33:29] default.INFO: 172.18.0.1 POST 127.0.0.1:8787/demo/sql [707.586ms] [webman/log]
[POST] array ()
[SQL] CONNECT: [ UseTime:0.000798s ] mysql:host=dnmp-mysql;port=3306;dbname=nacos;charset=utf8
[SQL] SHOW FULL COLUMNS FROM `config_info` [ RunTime:0.006718s ]
[SQL] SELECT * FROM `config_info` WHERE `id` = 1 [ RunTime:0.000692s ]
[SQL] SHOW FULL COLUMNS FROM `config_tags_relation` [ RunTime:0.002865s ]
[SQL] SELECT * FROM `config_tags_relation` [ RunTime:0.000390s ]Important Note
If the thinkorm plugin does not generate logs, manually set trigger_sql to true in config/thinkorm.php. The log plugin does not interfere with Webman's core logging, but enabling both webman/log and the SQL trigger may produce duplicate exception logs.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
