Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 控制服务的accesslog数量 #9675 #9684

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/bkenv.properties
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ BK_CI_DEVOPS_TOKEN=devops
BK_ESB_ENABLED=false
# BK_ESB_HOST
BK_ESB_HOST=
# BK_CI_PUBLIC_PATH
BK_CI_PUBLIC_PATH=

##########
# 2-公共依赖
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
<file>${service.log.dir}/service.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${service.log.dir}/service.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<maxHistory>30</maxHistory>
<maxHistory>3</maxHistory>
<maxFileSize>1GB</maxFileSize>
<totalSizeCap>10GB</totalSizeCap>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>${pattern}</pattern>
Expand All @@ -33,9 +33,9 @@
<file>${service.log.dir}/service-error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${service.log.dir}/service-error.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<maxHistory>30</maxHistory>
<maxHistory>3</maxHistory>
<maxFileSize>1GB</maxFileSize>
<totalSizeCap>10GB</totalSizeCap>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>${pattern}</pattern>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.tencent.devops.common.service.config.CommonConfig
import com.tencent.devops.common.web.interceptor.BkWriterInterceptor
import com.tencent.devops.common.web.jasypt.DefaultEncryptor
import com.tencent.devops.common.web.runner.BkServiceInstanceApplicationRunner
import com.tencent.devops.common.web.task.AccessLogCleanupTask
import io.micrometer.core.instrument.binder.jersey.server.JerseyTagsProvider
import io.undertow.UndertowOptions
import org.slf4j.LoggerFactory
Expand Down Expand Up @@ -73,6 +74,9 @@ import org.springframework.core.env.Environment
@DependsOn("globalProxyConfiguration")
class WebAutoConfiguration {

@Value("\${server.undertow.accesslog.dir}")
private val undertowAccessLogDir: String = ""

@Bean
@Profile("prod")
fun jerseyConfig() = JerseyConfig()
Expand Down Expand Up @@ -139,5 +143,8 @@ class WebAutoConfiguration {
return factory
}

@Bean
fun accessLogCleanUpTask() = AccessLogCleanupTask(undertowAccessLogDir)

private val logger = LoggerFactory.getLogger(WebAutoConfiguration::class.java)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.tencent.devops.common.web.task

import org.slf4j.LoggerFactory
import org.springframework.scheduling.annotation.Scheduled
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path

import java.nio.file.Paths
import java.util.stream.Collectors

/**
* 定时清理访问日志
*/
@SuppressWarnings("NestedBlockDepth")
class AccessLogCleanupTask(
logDirectory: String
) {
private val logDirectory: Path

init {
this.logDirectory = Paths.get(logDirectory)
}

@Scheduled(cron = "0 0 1 * * ?")
fun cleanupLogs() {
try {
Files.list(logDirectory).use { stream ->
val logFiles = stream.filter { path ->
val fileName = path.fileName.toString()
fileName.startsWith("access_log") && fileName != "access_log.log"
}.sorted(Comparator.comparing { path -> -path.toFile().lastModified() }).collect(Collectors.toList())

if (logFiles.size > MAX_LOG_FILES) {
for (i in MAX_LOG_FILES until logFiles.size) {
Files.delete(logFiles[i])
logger.info("Deleted old access log file: {}", logFiles[i])
}
}
}
} catch (e: IOException) {
logger.error("Error cleaning up access log files", e)
}
}

companion object {
private val logger = LoggerFactory.getLogger(AccessLogCleanupTask::class.java)
private const val MAX_LOG_FILES = 3 // 保留个数
}
}