Skip to content

设置日志文件路径 - LogSetFilePath

函数简介

设置日志文件的保存路径。

接口名称

LogSetFilePath

DLL调用

c
int32_t LogSetFilePath(int64_t instance, int64_t loggerHandle, const char* logFilePath);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
loggerHandle长整数型日志实例句柄(0 表示默认实例)。
logFilePath字符串日志文件路径(为空则使用默认路径)。

示例

LogSetFilePath 设置完整文件路径;若只用文件名模式,请改用 LogSetBaseDirectory + LogSetFileNamePattern

SDK 调用

cpp
#include "OLAPlugServer.h"
#include <cstdio>

OLAPlugServer ola;
const int64_t logger = 0; // 默认实例

// 固定写到指定文件(完整路径,不是 FileNamePattern)
int ret = ola.LogSetFilePath(logger, "D:\\logs\\app.log");
if (ret != 1) {
    return;
}
ola.LogInfo("writing to fixed path");
std::string path = ola.LogGetCurrentFilePath(logger);
std::printf("current log file: %s\n", path.c_str());
csharp
using System;
using OLAPlug;

var ola = new OLAPlugServer();
long logger = 0;

// 固定写到指定文件(完整路径,不是 FileNamePattern)
int ret = ola.LogSetFilePath(logger, @"D:\logs\app.log");
if (ret != 1) return;
ola.LogInfo("writing to fixed path");
string path = ola.LogGetCurrentFilePath(logger);
Console.WriteLine("current log file: " + path);
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
logger = 0

# 固定写到指定文件(完整路径,不是 FileNamePattern)
ret = ola.LogSetFilePath(logger, r"D:\logs\app.log")
if ret != 1:
    raise RuntimeError("failed")
ola.LogInfo("writing to fixed path")
path = ola.LogGetCurrentFilePath(logger)
print("current log file:", path)
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
long logger = 0;

// 固定写到指定文件(完整路径,不是 FileNamePattern)
int ret = ola.LogSetFilePath(logger, "D:\\logs\\app.log");
if (ret != 1) {
    return;
}
ola.LogInfo("writing to fixed path");
String path = ola.LogGetCurrentFilePath(logger);
System.out.println("current log file: " + path);
go
import (
    "fmt"
    "github.com/ola/olaplug/olaplug"
)

ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
logger := int64(0)

// 固定写到指定文件(完整路径,不是 FileNamePattern)
ret := ola.LogSetFilePath(logger, `D:\logs\app.log`)
if ret != 1 {
    return
}
ola.LogInfo("writing to fixed path")
path := ola.LogGetCurrentFilePath(logger)
fmt.Println("current log file:", path)
rust
use olaplug::OLAPlugServer;

let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let logger = 0i64;

// 固定写到指定文件(完整路径,不是 FileNamePattern)
let ret = ola.log_set_file_path(logger, r"D:\logs\app.log");
if ret != 1 {
    return;
}
ola.log_info("writing to fixed path");
let path = ola.log_get_current_file_path(logger);
println!("current log file: {}", path);
cpp
var ola = com("OlaPlug.OlaSoft")
var logger = 0

// 固定写到指定文件(完整路径,不是 FileNamePattern)
var ret = ola.LogSetFilePath(logger, "D:\\logs\\app.log")
if(ret == 1) {
    ola.LogInfo("writing to fixed path")
    var path = ola.LogGetCurrentFilePath(logger)
    traceprint("current log file: " + path)
}
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
logger = 0

' 固定写到指定文件(完整路径,不是 FileNamePattern)
ret = ola.LogSetFilePath(logger, "D:\logs\app.log")
If ret = 1 Then
    ola.LogInfo "writing to fixed path"
    path = ola.LogGetCurrentFilePath(logger)
    TracePrint "current log file: " & path
End If
text
.局部变量 ola, OLAPlug
.局部变量 ret, 整数型
.局部变量 logger, 长整数型
.局部变量 path, 文本型

ola.Create ()
logger = 0

' 固定写到指定文件(完整路径,不是 FileNamePattern)
ret = ola.LogSetFilePath (logger, “D:\logs\app.log”)
.如果真 (ret = 1)
    ola.LogInfo (“writing to fixed path”)
    path = ola.LogGetCurrentFilePath (logger)
    调试输出 (“current log file:”, path)
.如果真结束
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var logger = 0;

// 固定写到指定文件(完整路径,不是 FileNamePattern)
var ret = ola.LogSetFilePath(logger, "D:\\logs\\app.log");
if(ret == 1){
    ola.LogInfo("writing to fixed path");
    var path = ola.LogGetCurrentFilePath(logger);
    io.print("current log file: ", path);
}
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
长整数 logger = 0

' 固定写到指定文件(完整路径,不是 FileNamePattern)
整数 ret = ola.LogSetFilePath(logger, "D:\\logs\\app.log")
如果真 (ret = 1)
{
    ola.LogInfo("writing to fixed path")
    文本 path = ola.LogGetCurrentFilePath(logger)
    调试输出 ("current log file:", path)
}
cpp
#include "OLAPlugServer.h"
#include <cstdio>

OLAPlugServer ola;
int64_t logger = 0;

// 固定写到指定文件(完整路径,不是 FileNamePattern)
int32_t ret = ola.LogSetFilePath(logger, "D:\\logs\\app.log");
if (ret != 1) {
    return;
}
ola.LogInfo("writing to fixed path");
std::string path = ola.LogGetCurrentFilePath(logger);
std::printf("current log file: %s\n", path.c_str());

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long logger = 0;

int ret = LogSetFilePath(instance, logger, "D:\\logs\\app.log");
if (ret != 1) {
    return;
}
LogInfo(instance, "writing to fixed path");
// LogGetCurrentFilePath(instance, logger) → D:\logs\app.log
csharp
using System.Runtime.InteropServices;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int LogSetFilePath(long ola, long loggerHandle, string logFilePath);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int LogInfo(long ola, string message);

long instance = CreateCOLAPlugInterFace();
long logger = 0;

int ret = LogSetFilePath(instance, logger, @"D:\logs\app.log");
if (ret != 1) return;
LogInfo(instance, "writing to fixed path");
python
from ctypes import CDLL, c_int, c_int64

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
logger = 0

ret = ola.LogSetFilePath(instance, logger, b"D:\\logs\\app.log")
if ret != 1:
    raise RuntimeError("LogSetFilePath failed")
ola.LogInfo(instance, b"writing to fixed path")

返回值

返回值说明
1成功。
0失败。

注意事项

项目说明
默认路径./user_logs/app.log(程序运行目录下)。
修改后立即生效修改后立即生效,如果日志系统已初始化,会自动重新初始化。
如果目录不存在如果目录不存在,系统会自动创建。
路径支持相对路径和绝对路径。
路径建议配合 LogSetBaseDirectoryLogSetFileNamePattern 使用,实现更灵活的路径管理。