Skip to content

设置文件分割模式 - LogSetRotationMode

函数简介

设置日志文件的分割模式,支持按大小、按日期、按小时等多种方式分割日志文件。

接口名称

LogSetRotationMode

DLL调用

c
int32_t LogSetRotationMode(int64_t instance, int64_t loggerHandle, int32_t rotationMode);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
loggerHandle长整数型日志实例句柄(0 表示默认实例)。
rotationMode整数型分割模式(可位或组合),见 LogRotationMode 枚举。

LogRotationMode 枚举值

枚举值说明
OLA_LOG_ROTATION_NONE0不分割(单文件,不推荐)
OLA_LOG_ROTATION_SIZE1按大小分割(默认启用)
OLA_LOG_ROTATION_DAILY2每日分割
OLA_LOG_ROTATION_HOURLY4每小时分割

位运算组合示例

将需要的分割模式 按位或(|)相加 后传入 rotationMode(满足任一条件即触发分割):

csharp
// 按大小 + 每日分割
int rotationMode = 1 | 2;  // OLA_LOG_ROTATION_SIZE | OLA_LOG_ROTATION_DAILY
ola.LogSetRotationMode(0, rotationMode);

// 三种模式全部启用
int allModes = 1 | 2 | 4;  // 7
ola.LogSetRotationMode(0, allModes);
cpp
// 按大小 + 每小时分割
int rotationMode = 1 | 4;  // 5
ola.LogSetRotationMode(0, rotationMode);
python
# 仅按大小分割(默认)
ola.LogSetRotationMode(0, 1)

# 按大小 + 每日分割
ola.LogSetRotationMode(0, 1 | 2)

示例

Rotation:SIZE=1 DAILY=2 HOURLY=4。示例:1|2(按大小+每日),配合 MaxFileSize 与 {index}

SDK 调用

cpp
#include "OLAPlugServer.h"

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

ola.LogSetBaseDirectory(logger, "D:\\logs");
ola.LogSetFileNamePattern(logger, "app_{date}_{index}.log");
int ret = ola.LogSetRotationMode(logger, 1 | 2); // SIZE | DAILY
if (ret != 1) {
    return;
}
ola.LogSetMaxFileSize(logger, 50); // 50 MB
ola.LogInfo("rotation SIZE|DAILY");
csharp
using System;
using OLAPlug;

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

ola.LogSetBaseDirectory(logger, @"D:\logs");
ola.LogSetFileNamePattern(logger, "app_{date}_{index}.log");
int ret = ola.LogSetRotationMode(logger, 1 | 2); // SIZE | DAILY
if (ret != 1) return;
ola.LogSetMaxFileSize(logger, 50); // 50 MB
ola.LogInfo("rotation SIZE|DAILY");
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
logger = 0

ola.LogSetBaseDirectory(logger, r"D:\logs")
ola.LogSetFileNamePattern(logger, "app_{date}_{index}.log")
ret = ola.LogSetRotationMode(logger, 1 | 2)  # SIZE | DAILY
if ret != 1:
    raise RuntimeError("failed")
ola.LogSetMaxFileSize(logger, 50)  # 50 MB
ola.LogInfo("rotation SIZE|DAILY")
java
import com.olaplug.OLAPlugServer;

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

ola.LogSetBaseDirectory(logger, "D:\\logs");
ola.LogSetFileNamePattern(logger, "app_{date}_{index}.log");
int ret = ola.LogSetRotationMode(logger, 1 | 2); // SIZE | DAILY
if (ret != 1) {
    return;
}
ola.LogSetMaxFileSize(logger, 50); // 50 MB
ola.LogInfo("rotation SIZE|DAILY");
go
import "github.com/ola/olaplug/olaplug"

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

ola.LogSetBaseDirectory(logger, `D:\logs`)
ola.LogSetFileNamePattern(logger, "app_{date}_{index}.log")
ret := ola.LogSetRotationMode(logger, 1 | 2) // SIZE | DAILY
if ret != 1 {
    return
}
ola.LogSetMaxFileSize(logger, 50) // 50 MB
ola.LogInfo("rotation SIZE|DAILY")
rust
use olaplug::OLAPlugServer;

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

ola.log_set_base_directory(logger, r"D:\logs");
ola.log_set_file_name_pattern(logger, "app_{date}_{index}.log");
let ret = ola.log_set_rotation_mode(logger, 1 | 2); // SIZE | DAILY
if ret != 1 {
    return;
}
ola.log_set_max_file_size(logger, 50); // 50 MB
ola.log_info("rotation SIZE|DAILY");
cpp
var ola = com("OlaPlug.OlaSoft")
var logger = 0

ola.LogSetBaseDirectory(logger, "D:\\logs")
ola.LogSetFileNamePattern(logger, "app_{date}_{index}.log")
var ret = ola.LogSetRotationMode(logger, 1 | 2) // SIZE | DAILY
if(ret == 1) {
    ola.LogSetMaxFileSize(logger, 50) // 50 MB
    ola.LogInfo("rotation SIZE|DAILY")
}
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
logger = 0

ola.LogSetBaseDirectory logger, "D:\logs"
ola.LogSetFileNamePattern logger, "app_{date}_{index}.log"
ret = ola.LogSetRotationMode(logger, 3)
If ret = 1 Then
    ola.LogSetMaxFileSize logger, 50
    ola.LogInfo "rotation SIZE|DAILY"
End If
text
.局部变量 ola, OLAPlug
.局部变量 ret, 整数型
.局部变量 logger, 长整数型

ola.Create ()
logger = 0

ola.LogSetBaseDirectory (logger, “D:\logs”)
ola.LogSetFileNamePattern (logger, “app_{date}_{index}.log”)
ret = ola.LogSetRotationMode (logger, 位或 (1, 2))  ' SIZE | DAILY
.如果真 (ret = 1)
    ola.LogSetMaxFileSize (logger, 50)  ' 50 MB
    ola.LogInfo (“rotation SIZE|DAILY”)
.如果真结束
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var logger = 0;

ola.LogSetBaseDirectory(logger, "D:\\logs");
ola.LogSetFileNamePattern(logger, "app_{date}_{index}.log");
var ret = ola.LogSetRotationMode(logger, 1 | 2); // SIZE | DAILY
if(ret == 1){
    ola.LogSetMaxFileSize(logger, 50); // 50 MB
    ola.LogInfo("rotation SIZE|DAILY");
}
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
长整数 logger = 0

ola.LogSetBaseDirectory(logger, "D:\\logs")
ola.LogSetFileNamePattern(logger, "app_{date}_{index}.log")
整数 ret = ola.LogSetRotationMode(logger, 1 位或 2)  ' SIZE | DAILY
如果真 (ret = 1)
{
    ola.LogSetMaxFileSize(logger, 50)  ' 50 MB
    ola.LogInfo("rotation SIZE|DAILY")
}
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
int64_t logger = 0;

ola.LogSetBaseDirectory(logger, "D:\\logs");
ola.LogSetFileNamePattern(logger, "app_{date}_{index}.log");
int32_t ret = ola.LogSetRotationMode(logger, 1 | 2); // SIZE | DAILY
if (ret != 1) {
    return;
}
ola.LogSetMaxFileSize(logger, 50); // 50 MB
ola.LogInfo("rotation SIZE|DAILY");

原生 DLL 调用

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

LogSetBaseDirectory(instance, logger, "D:\\logs");
LogSetFileNamePattern(instance, logger, "app_{date}_{index}.log");
int ret = LogSetRotationMode(instance, logger, 1 | 2); // SIZE | DAILY
if (ret != 1) {
    return;
}
LogSetMaxFileSize(instance, logger, 50);
LogInfo(instance, "rotation SIZE|DAILY");
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 LogSetBaseDirectory(long ola, long loggerHandle, string baseDirectory);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int LogSetFileNamePattern(long ola, long loggerHandle, string fileNamePattern);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int LogSetRotationMode(long ola, long loggerHandle, int rotationMode);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int LogSetMaxFileSize(long ola, long loggerHandle, int maxFileSizeMb);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int LogInfo(long ola, string message);

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

LogSetBaseDirectory(instance, logger, @"D:\logs");
LogSetFileNamePattern(instance, logger, "app_{date}_{index}.log");
int ret = LogSetRotationMode(instance, logger, 1 | 2);
if (ret != 1) return;
LogSetMaxFileSize(instance, logger, 50);
LogInfo(instance, "rotation SIZE|DAILY");
python
from ctypes import CDLL, c_int, c_int64

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

ola.LogSetBaseDirectory(instance, logger, b"D:\\logs")
ola.LogSetFileNamePattern(instance, logger, b"app_{date}_{index}.log")
ret = ola.LogSetRotationMode(instance, logger, 1 | 2)
if ret != 1:
    raise RuntimeError("LogSetRotationMode failed")
ola.LogSetMaxFileSize(instance, logger, 50)
ola.LogInfo(instance, b"rotation SIZE|DAILY")

返回值

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

注意事项

项目说明
默认值OLA_LOG_ROTATION_SIZE (1) - 仅按大小分割。
可以使用位或运算(``)组合多种模式
LogSetMaxFileSize按大小分割的阈值通过 LogSetMaxFileSize 设置(默认 100MB)。
LogSetMaxFiles分割后的旧文件数量通过 LogSetMaxFiles 控制(默认保留 10 个)。
修改后立即生效修改后立即生效,如果日志系统已初始化,会自动重新初始化。
组合使用时组合使用时,满足任一条件都会触发分割。