@@ -11,65 +11,74 @@ use stackable_operator::{
1111} ;
1212
1313use crate :: crd:: {
14- STACKABLE_LOG_DIR ,
1514 role:: { AnyConfig , broker:: BrokerContainer , controller:: ControllerContainer } ,
1615 v1alpha1,
1716} ;
1817
18+ pub const STACKABLE_LOG_CONFIG_DIR : & str = "/stackable/log_config" ;
19+ pub const STACKABLE_LOG_DIR : & str = "/stackable/log" ;
20+ // log4j
1921pub const LOG4J_CONFIG_FILE : & str = "log4j.properties" ;
20- pub const KAFKA_LOG_FILE : & str = "kafka.log4j.xml" ;
21-
22+ pub const KAFKA_LOG4J_FILE : & str = "kafka.log4j.xml" ;
23+ // log4j2
24+ pub const LOG4J2_CONFIG_FILE : & str = "log4j2.properties" ;
25+ pub const KAFKA_LOG4J2_FILE : & str = "kafka.log4j2.xml" ;
26+ // max size
2227pub const MAX_KAFKA_LOG_FILES_SIZE : MemoryQuantity = MemoryQuantity {
2328 value : 10.0 ,
2429 unit : BinaryMultiple :: Mebi ,
2530} ;
2631
27- const CONSOLE_CONVERSION_PATTERN : & str = "[%d] %p %m (%c)%n" ;
32+ const CONSOLE_CONVERSION_PATTERN_LOG4J : & str = "[%d] %p %m (%c)%n" ;
33+ const CONSOLE_CONVERSION_PATTERN_LOG4J2 : & str = "%d{ISO8601} %p [%t] %c - %m%n" ;
34+
35+ pub fn kafka_log_opts ( product_version : & str ) -> String {
36+ if product_version. starts_with ( "4." ) {
37+ format ! ( "-Dlog4j2.configuration=file:{STACKABLE_LOG_CONFIG_DIR}/{LOG4J2_CONFIG_FILE}" )
38+ } else {
39+ format ! ( "-Dlog4j.configuration=file:{STACKABLE_LOG_CONFIG_DIR}/{LOG4J_CONFIG_FILE}" )
40+ }
41+ }
42+
43+ pub fn kafka_log_opts_env_var ( product_version : & str ) -> String {
44+ if product_version. starts_with ( "4." ) {
45+ "KAFKA_LOG4J2_OPTS" . to_string ( )
46+ } else {
47+ "KAFKA_LOG4J_OPTS" . to_string ( )
48+ }
49+ }
2850
2951/// Extend the role group ConfigMap with logging and Vector configurations
3052pub fn extend_role_group_config_map (
53+ product_version : & str ,
3154 rolegroup : & RoleGroupRef < v1alpha1:: KafkaCluster > ,
3255 merged_config : & AnyConfig ,
3356 cm_builder : & mut ConfigMapBuilder ,
3457) {
35- fn add_log4j_config_if_automatic (
36- cm_builder : & mut ConfigMapBuilder ,
37- log_config : Option < Cow < ContainerLogConfig > > ,
38- log_config_file : & str ,
39- container_name : impl Display ,
40- log_file : & str ,
41- max_log_file_size : MemoryQuantity ,
42- ) {
43- if let Some ( ContainerLogConfig {
44- choice : Some ( ContainerLogConfigChoice :: Automatic ( log_config) ) ,
45- } ) = log_config. as_deref ( )
46- {
47- cm_builder. add_data (
48- log_config_file,
49- product_logging:: framework:: create_log4j_config (
50- & format ! ( "{STACKABLE_LOG_DIR}/{container_name}" ) ,
51- log_file,
52- max_log_file_size
53- . scale_to ( BinaryMultiple :: Mebi )
54- . floor ( )
55- . value as u32 ,
56- CONSOLE_CONVERSION_PATTERN ,
57- log_config,
58- ) ,
59- ) ;
60- }
58+ let container_name = match merged_config {
59+ AnyConfig :: Broker ( _) => BrokerContainer :: Kafka . to_string ( ) ,
60+ AnyConfig :: Controller ( _) => ControllerContainer :: Kafka . to_string ( ) ,
61+ } ;
62+
63+ // Starting with Kafka 4.0, log4j2 is used instead of log4j.
64+ match product_version. starts_with ( "4." ) {
65+ true => add_log4j2_config_if_automatic (
66+ cm_builder,
67+ Some ( merged_config. kafka_logging ( ) ) ,
68+ LOG4J2_CONFIG_FILE ,
69+ container_name,
70+ KAFKA_LOG4J2_FILE ,
71+ MAX_KAFKA_LOG_FILES_SIZE ,
72+ ) ,
73+ false => add_log4j_config_if_automatic (
74+ cm_builder,
75+ Some ( merged_config. kafka_logging ( ) ) ,
76+ LOG4J_CONFIG_FILE ,
77+ container_name,
78+ KAFKA_LOG4J_FILE ,
79+ MAX_KAFKA_LOG_FILES_SIZE ,
80+ ) ,
6181 }
62- add_log4j_config_if_automatic (
63- cm_builder,
64- Some ( merged_config. kafka_logging ( ) ) ,
65- LOG4J_CONFIG_FILE ,
66- match merged_config {
67- AnyConfig :: Broker ( _) => BrokerContainer :: Kafka . to_string ( ) ,
68- AnyConfig :: Controller ( _) => ControllerContainer :: Kafka . to_string ( ) ,
69- } ,
70- KAFKA_LOG_FILE ,
71- MAX_KAFKA_LOG_FILES_SIZE ,
72- ) ;
7382
7483 let vector_log_config = merged_config. vector_logging ( ) ;
7584 let vector_log_config = if let ContainerLogConfig {
@@ -88,3 +97,59 @@ pub fn extend_role_group_config_map(
8897 ) ;
8998 }
9099}
100+
101+ fn add_log4j_config_if_automatic (
102+ cm_builder : & mut ConfigMapBuilder ,
103+ log_config : Option < Cow < ContainerLogConfig > > ,
104+ log_config_file : & str ,
105+ container_name : impl Display ,
106+ log_file : & str ,
107+ max_log_file_size : MemoryQuantity ,
108+ ) {
109+ if let Some ( ContainerLogConfig {
110+ choice : Some ( ContainerLogConfigChoice :: Automatic ( log_config) ) ,
111+ } ) = log_config. as_deref ( )
112+ {
113+ cm_builder. add_data (
114+ log_config_file,
115+ product_logging:: framework:: create_log4j_config (
116+ & format ! ( "{STACKABLE_LOG_DIR}/{container_name}" ) ,
117+ log_file,
118+ max_log_file_size
119+ . scale_to ( BinaryMultiple :: Mebi )
120+ . floor ( )
121+ . value as u32 ,
122+ CONSOLE_CONVERSION_PATTERN_LOG4J ,
123+ log_config,
124+ ) ,
125+ ) ;
126+ }
127+ }
128+
129+ fn add_log4j2_config_if_automatic (
130+ cm_builder : & mut ConfigMapBuilder ,
131+ log_config : Option < Cow < ContainerLogConfig > > ,
132+ log_config_file : & str ,
133+ container_name : impl Display ,
134+ log_file : & str ,
135+ max_log_file_size : MemoryQuantity ,
136+ ) {
137+ if let Some ( ContainerLogConfig {
138+ choice : Some ( ContainerLogConfigChoice :: Automatic ( log_config) ) ,
139+ } ) = log_config. as_deref ( )
140+ {
141+ cm_builder. add_data (
142+ log_config_file,
143+ product_logging:: framework:: create_log4j2_config (
144+ & format ! ( "{STACKABLE_LOG_DIR}/{container_name}" , ) ,
145+ log_file,
146+ max_log_file_size
147+ . scale_to ( BinaryMultiple :: Mebi )
148+ . floor ( )
149+ . value as u32 ,
150+ CONSOLE_CONVERSION_PATTERN_LOG4J2 ,
151+ log_config,
152+ ) ,
153+ ) ;
154+ }
155+ }
0 commit comments