Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
templateService
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Bazarbay Tulenov
templateService
Commits
e622debc
Commit
e622debc
authored
Jan 19, 2024
by
George Novikov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
копирование файлов лога при превышении размера в конфиге
parent
e04aad89
Pipeline
#367
failed with stage
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
6 deletions
+57
-6
src/main/java/kz/project/printedFormsService/logging/ProcessLogger.java
...kz/project/printedFormsService/logging/ProcessLogger.java
+55
-5
src/main/resources/application.yml
src/main/resources/application.yml
+2
-1
No files found.
src/main/java/kz/project/printedFormsService/logging/ProcessLogger.java
View file @
e622debc
...
...
@@ -2,6 +2,7 @@ package kz.project.printedFormsService.logging;
import
jakarta.servlet.http.HttpServletRequest
;
import
kz.project.printedFormsService.converter.JwtAuthConverter
;
import
org.apache.tomcat.util.http.fileupload.FileUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Value
;
...
...
@@ -12,7 +13,13 @@ import org.springframework.web.context.request.RequestContextHolder;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
java.io.File
;
import
java.io.FileNotFoundException
;
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.nio.file.StandardCopyOption
;
import
java.time.Instant
;
import
java.time.ZoneId
;
import
java.time.ZonedDateTime
;
...
...
@@ -24,10 +31,16 @@ import java.util.Locale;
public
class
ProcessLogger
{
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
ProcessLogger
.
class
);
private
static
final
String
DEFAULT_FILENAME
=
"siem-processes.log"
;
private
static
final
int
DEFAULT_FILE_SIZE
=
100
;
private
static
final
String
DEFAULT_DELIMITER
=
" "
;
private
static
final
String
DEFAULT_START_PREFIX
=
"Process start: "
;
private
static
final
String
DEFAULT_FINISH_PREFIX
=
"Process finish: "
;
private
static
final
String
DEFAULT_ERROR_PREFIX
=
"Process error: "
;
@Value
(
"${process-logger.file-name}"
)
private
String
logFileName
;
@Value
(
"${process-logger.file-max-size-mb}"
)
private
int
logFileSize
;
@Value
(
"${process-logger.delimiter}"
)
private
String
delimiter
;
@Value
(
"${spring.application.name}"
)
...
...
@@ -50,23 +63,24 @@ public class ProcessLogger {
}
public
void
start
(
Class
javaClass
,
String
processName
,
String
description
){
write
(
javaClass
,
processName
,
startPrefix
,
description
);
write
(
javaClass
,
processName
,
startPrefix
!=
null
?
startPrefix
:
DEFAULT_START_PREFIX
,
description
);
}
public
void
finish
(
Class
javaClass
,
String
processName
,
String
description
){
write
(
javaClass
,
processName
,
finishPrefix
,
description
);
write
(
javaClass
,
processName
,
finishPrefix
!=
null
?
finishPrefix
:
DEFAULT_FINISH_PREFIX
,
description
);
}
public
void
error
(
Class
javaClass
,
String
processName
,
String
description
){
write
(
javaClass
,
processName
,
errorPrefix
,
description
);
write
(
javaClass
,
processName
,
errorPrefix
!=
null
?
errorPrefix
:
DEFAULT_ERROR_PREFIX
,
description
);
}
public
void
write
(
Class
javaClass
,
String
processName
,
String
descriptionPrefix
,
String
description
){
String
delimiter
=
this
.
delimiter
!=
null
?
this
.
delimiter
:
DEFAULT_DELIMITER
;
String
fileName
=
this
.
logFileName
!=
null
?
this
.
logFileName
:
DEFAULT_FILENAME
;
try
{
HttpServletRequest
request
=
getRequest
();
String
delimiter
=
this
.
delimiter
!=
null
?
this
.
delimiter
:
DEFAULT_DELIMITER
;
String
fileName
=
this
.
logFileName
!=
null
?
this
.
logFileName
:
DEFAULT_FILENAME
;
String
ip
=
request
.
getRemoteAddr
();
String
userName
=
null
;
...
...
@@ -96,16 +110,52 @@ public class ProcessLogger {
);
File
file
=
new
File
(
fileName
);
controlFileSize
(
file
);
boolean
isCreated
=
file
.
createNewFile
();
if
(!
isValidFile
(
file
))
return
;
FileWriter
writer
=
new
FileWriter
(
fileName
,
true
);
writer
.
append
(
logRow
).
append
(
System
.
lineSeparator
());
writer
.
close
();
}
catch
(
FileNotFoundException
fnfe
){
LOGGER
.
error
(
"Ошибка чтения/записи файла {}. Недостаточно прав."
,
fileName
);
}
catch
(
Exception
e
){
LOGGER
.
error
(
e
.
getMessage
(),
e
);
}
}
private
boolean
isValidFile
(
File
file
){
boolean
isValid
=
file
.
isFile
();
if
(!
isValid
)
LOGGER
.
warn
(
"{} не является файлом. Проверьте конфигурацию."
,
file
.
getName
());
return
isValid
;
}
private
boolean
isOverwhelmed
(
File
file
){
int
maxSizeMegabytes
=
logFileSize
>
0
?
logFileSize
:
DEFAULT_FILE_SIZE
;
long
fileSizeMegabytes
=
file
.
length
()
/
1024
*
1024
;
return
fileSizeMegabytes
>=
maxSizeMegabytes
;
}
private
void
copyFile
(
File
file
)
throws
IOException
{
String
filePath
=
this
.
logFileName
!=
null
?
this
.
logFileName
:
DEFAULT_FILENAME
;
String
todayString
=
getTodayDateTimeString
();
String
copyFilePath
=
String
.
format
(
"%s%s"
,
filePath
,
todayString
.
replace
(
" "
,
""
).
replace
(
"."
,
""
).
replace
(
":"
,
""
));
Path
copyPath
=
Paths
.
get
(
copyFilePath
);
Path
originalPath
=
file
.
toPath
();
Files
.
copy
(
originalPath
,
copyPath
,
StandardCopyOption
.
REPLACE_EXISTING
);
LOGGER
.
info
(
"Файл лога {} превысил допустимый размер, и был скопирован в {}"
,
filePath
,
copyFilePath
);
}
private
void
controlFileSize
(
File
file
)
throws
IOException
{
if
(
isOverwhelmed
(
file
)){
copyFile
(
file
);
file
.
delete
();
}
}
private
String
getTodayDateTimeString
(){
Instant
instant
=
new
Date
().
toInstant
();
ZonedDateTime
zonedDateTime
=
instant
.
atZone
(
this
.
zoneId
);
...
...
src/main/resources/application.yml
View file @
e622debc
...
...
@@ -29,7 +29,7 @@ spring:
datasource
:
url
:
jdbc:postgresql://${DB_HOST:localhost}:${DB_PORT:5432}/${DB_NAME:template_db}?currentSchema=template_schema
username
:
${DATASOURCE_USER:postgres}
password
:
${DATASOURCE_PASSWORD:
password
}
password
:
${DATASOURCE_PASSWORD:
root
}
hikari
:
maximum-pool-size
:
${MAXIMUM_POOL_SIZE:100}
minimum-idle
:
2
...
...
@@ -76,6 +76,7 @@ management:
process-logger
:
file-name
:
templateService.log
file-max-size-mb
:
100
delimiter
:
"
"
date-format
:
"
dd.MM.yyyy
HH:mm:ss"
locale
:
kk_KZ
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment