You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Delphi 11.3 macOS Release模式下如何查看Log.d()输出?

在Delphi 11.3 macOS Release模式下查看Log.d()输出的方法

调试模式下Log.d()依赖PAServer传递日志,但Release模式脱离Delphi后默认没有这个通道,你可以用以下几种方式捕获日志:

1. 将日志写入本地文件

直接把日志输出到应用沙盒内的文件,后续可通过访达找到文件查看。

你可以自定义日志函数替代Log.d(),或通过编译条件在Release模式下重定向日志行为:

uses System.SysUtils, System.IOUtils;

procedure CustomLog(const Msg: string);
var
  LogFile: TextFile;
  LogFilePath: string;
begin
  // 获取应用沙盒的Documents目录路径
  LogFilePath := TPath.Combine(TPath.GetDocumentsPath, 'app_release_log.txt');
  
  AssignFile(LogFile, LogFilePath);
  try
    if FileExists(LogFilePath) then
      Append(LogFile)
    else
      Rewrite(LogFile);
    // 追加时间戳方便排查问题
    Writeln(LogFile, FormatDateTime('yyyy-MM-dd HH:mm:ss', Now) + ' | ' + Msg);
  finally
    CloseFile(LogFile);
  end;
end;

// 代码中通过编译条件切换日志输出
{$IFDEF RELEASE}
  CustomLog('Release mode log content');
{$ELSE}
  Log.d('Debug mode log content');
{$ENDIF}

之后通过访达的「前往文件夹」输入~/Library/Containers/[你的应用Bundle ID]/Data/Documents,就能找到生成的日志文件。

2. 输出到macOS系统控制台

利用macOS系统日志框架,让日志直接显示在系统「控制台」应用中,无需额外文件操作。

可以调用POSIX的syslog接口实现:

uses Posix.Syslog;

procedure SysLog(const Msg: string);
begin
  // 初始化日志,关联当前应用名称
  openlog(PAnsiChar(AnsiString(Application.Title)), LOG_PID or LOG_CONS, LOG_USER);
  // 输出INFO级别的日志条目
  syslog(LOG_INFO, '%s', [PAnsiChar(AnsiString(Msg))]);
  closelog;
end;

打开macOS控制台应用后,在搜索框输入你的应用名称,就能筛选出对应的日志,还能看到时间戳和进程ID。

3. 远程日志收集(实时查看)

如果需要实时监控Release模式的日志,可以搭建简单的TCP服务器,让应用主动把日志发送到服务端。

比如用Indy组件实现日志客户端:

uses IdTCPClient;

procedure SendRemoteLog(const Msg: string; const ServerIP: string; Port: Integer);
var
  TCPClient: TIdTCPClient;
begin
  TCPClient := TIdTCPClient.Create(nil);
  try
    TCPClient.Host := ServerIP;
    TCPClient.Port := Port;
    try
      TCPClient.Connect;
      TCPClient.IOHandler.WriteLn(FormatDateTime('yyyy-MM-dd HH:mm:ss', Now) + ' | ' + Msg);
      TCPClient.Disconnect;
    except
      // 捕获连接异常,避免应用崩溃
    end;
  finally
    TCPClient.Free;
  end;
end;

在本地或远程运行一个TCP服务端(比如用Python写个简单的接收脚本),就能实时收到应用发送的日志内容。


内容的提问来源于stack exchange,提问作者Mike at Bookup

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 05:42:27