在存储过程内部,是否可以将消息写入标准日志文件? 跟踪转到一个单独的文件,但我想指出一些在数据库的主日志文件中实际上并不是错误的内容。 它可以在不引发异常的情况下进行处理,但将信息记录在日志中以供以后使用会很方便。
是否可以做类似的事情:
log "Note this event happened."
虽然没有现有的 SQL 语句或函数可以直接写入服务器的消息日志,但 UTL_FILE 包也许可以用来实现类似的功能。有关此内容的文档可能稍微过时,因此这里是一个在 14.10.FC10 中有效的示例:
-- Register the datablade if this has not already been done:
execute function sysbldprepare("excompat.1.2", "create");
-- Open a file /tmp/example.log. Note that the file must exist for the append
-- option. The value returned is of type utl_file_file_type and was 0 for this
-- example. It would be better to use a stored procedure to capture the return
-- value and use it in the subsequent procedure calls.
execute function utl_file_fopen("/tmp", "example.log", "a", 1024);
-- Write a line of text that will be terminated with a newline
execute procedure utl_file_put_line(0::utl_file_file_type, "line 1 of text");
-- Write a line that does not have a trailing newline
execute procedure utl_file_put(0::utl_file_file_type, "line 2 of text w/o NL");
-- Write text that takes up to 5 varchar arguments. Only %s formats are
-- permitted. The output is terminated with a newline.
execute procedure utl_file_putf(0::utl_file_file_type,
"formatted text 2: %s %s", 2, "3rd arg");
-- Close the file
execute procedure utl_file_fclose(0::utl_file_file_type);
-- An alternative to close all open files.
execute procedure utl_file_fclose_all();
如果写入服务器的消息日志,写入活动之间不太可能有任何同步,因此并发写入文件可能会导致文本损坏。