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
adfe8c07
Commit
adfe8c07
authored
Jan 19, 2024
by
George Novikov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
установка прав на файл линукса в ProcessLogger
parent
e622debc
Pipeline
#368
canceled with stage
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
27 deletions
+61
-27
src/main/java/kz/project/printedFormsService/logging/ProcessLogger.java
...kz/project/printedFormsService/logging/ProcessLogger.java
+61
-27
No files found.
src/main/java/kz/project/printedFormsService/logging/ProcessLogger.java
View file @
adfe8c07
package
kz.project.printedFormsService.logging
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
jakarta.servlet.http.HttpServletRequest
;
import
kz.project.printedFormsService.converter.JwtAuthConverter
;
import
org.apache.tomcat.util.http.fileupload.FileUtils
;
...
...
@@ -20,11 +21,13 @@ import java.nio.file.Files;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.nio.file.StandardCopyOption
;
import
java.nio.file.attribute.PosixFilePermission
;
import
java.text.ParseException
;
import
java.time.Instant
;
import
java.time.ZoneId
;
import
java.time.ZonedDateTime
;
import
java.util.
Date
;
import
java.util.
Locale
;
import
java.util.
*
;
import
java.util.
stream.Collectors
;
/* Класс для логгирования старта и завершения процессов в формате SIEM и на естественном языке */
@Component
...
...
@@ -75,28 +78,43 @@ public class ProcessLogger {
}
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
ip
=
request
.
getRemoteAddr
();
String
userName
=
null
;
String
authHeader
=
request
.
getHeader
(
HttpHeaders
.
AUTHORIZATION
);
if
(
authHeader
!=
null
){
userName
=
JwtAuthConverter
.
getUsername
(
authHeader
.
substring
(
7
));
}
else
{
authHeader
=
request
.
getHeader
(
"X-API-Key"
);
if
(
authHeader
!=
null
)
userName
=
authHeader
.
substring
(
9
);
String
logRow
=
getLogRow
(
request
,
javaClass
.
getName
(),
processName
,
descriptionPrefix
,
description
,
fileName
);
File
file
=
new
File
(
fileName
);
controlFileSize
(
file
);
boolean
isCreated
=
file
.
createNewFile
();
if
(!
isValidFile
(
file
))
return
;
if
(
isUnixSystem
()){
setFilePermissions
(
file
);
}
String
logRow
=
String
.
format
(
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
String
getLogRow
(
HttpServletRequest
request
,
String
className
,
String
processName
,
String
descriptionPrefix
,
String
description
,
String
fileName
){
String
delimiter
=
this
.
delimiter
!=
null
?
this
.
delimiter
:
DEFAULT_DELIMITER
;
try
{
String
ip
=
request
.
getRemoteAddr
();
String
userName
=
getUserName
(
request
);
return
String
.
format
(
"%s%s%s.%s%s%s%s%s%s%s%s%s%s"
,
serviceName
,
delimiter
,
javaClass
.
getSimpleName
()
,
className
,
processName
,
delimiter
,
getTodayDateTimeString
(),
...
...
@@ -108,21 +126,24 @@ public class ProcessLogger {
descriptionPrefix
,
description
);
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
);
return
""
;
}
}
private
String
getUserName
(
HttpServletRequest
request
)
throws
ParseException
{
String
authHeader
=
request
.
getHeader
(
HttpHeaders
.
AUTHORIZATION
);
String
userName
=
null
;
if
(
authHeader
!=
null
){
userName
=
JwtAuthConverter
.
getUsername
(
authHeader
.
substring
(
7
));
}
else
{
authHeader
=
request
.
getHeader
(
"X-API-Key"
);
if
(
authHeader
!=
null
)
userName
=
authHeader
.
substring
(
9
);
}
return
userName
;
}
private
boolean
isValidFile
(
File
file
){
...
...
@@ -133,10 +154,18 @@ public class ProcessLogger {
private
boolean
isOverwhelmed
(
File
file
){
int
maxSizeMegabytes
=
logFileSize
>
0
?
logFileSize
:
DEFAULT_FILE_SIZE
;
long
fileSizeMegabytes
=
file
.
length
()
/
1024
*
1024
;
long
fileSizeMegabytes
=
file
.
length
()
/
(
1024
*
1024
);
LOGGER
.
info
(
"maxSizeMegabytes {}"
,
maxSizeMegabytes
);
LOGGER
.
info
(
"fileSizeMegabytes {}"
,
fileSizeMegabytes
);
LOGGER
.
info
(
"fileSizeMegabytes >= maxSizeMegabytes {}"
,
fileSizeMegabytes
>=
maxSizeMegabytes
);
return
fileSizeMegabytes
>=
maxSizeMegabytes
;
}
private
boolean
isUnixSystem
(){
String
osName
=
System
.
getProperty
(
"os.name"
).
toLowerCase
();
return
osName
.
contains
(
"nix"
)
||
osName
.
contains
(
"nux"
);
}
private
void
copyFile
(
File
file
)
throws
IOException
{
String
filePath
=
this
.
logFileName
!=
null
?
this
.
logFileName
:
DEFAULT_FILENAME
;
String
todayString
=
getTodayDateTimeString
();
...
...
@@ -156,6 +185,11 @@ public class ProcessLogger {
}
}
private
void
setFilePermissions
(
File
file
)
throws
IOException
{
Set
<
PosixFilePermission
>
permissions
=
Arrays
.
stream
(
PosixFilePermission
.
values
()).
collect
(
Collectors
.
toSet
());
Files
.
setPosixFilePermissions
(
file
.
toPath
(),
permissions
);
}
private
String
getTodayDateTimeString
(){
Instant
instant
=
new
Date
().
toInstant
();
ZonedDateTime
zonedDateTime
=
instant
.
atZone
(
this
.
zoneId
);
...
...
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