36
36
import org .apache .logging .log4j .core .config .Configuration ;
37
37
import org .apache .logging .log4j .core .config .Node ;
38
38
import org .apache .logging .log4j .core .config .plugins .Plugin ;
39
- import org .apache .logging .log4j .core .config .plugins .PluginAttribute ;
40
- import org .apache .logging .log4j .core .config .plugins .PluginConfiguration ;
39
+ import org .apache .logging .log4j .core .config .plugins .PluginBuilderAttribute ;
40
+ import org .apache .logging .log4j .core .config .plugins .PluginBuilderFactory ;
41
41
import org .apache .logging .log4j .core .config .plugins .PluginElement ;
42
- import org .apache .logging .log4j .core .config .plugins .PluginFactory ;
43
42
import org .apache .logging .log4j .core .layout .internal .ExcludeChecker ;
44
43
import org .apache .logging .log4j .core .layout .internal .IncludeChecker ;
45
44
import org .apache .logging .log4j .core .layout .internal .ListChecker ;
@@ -167,7 +166,7 @@ private Rfc5424Layout(
167
166
this .appName = appName ;
168
167
this .messageId = messageId ;
169
168
this .useTlsMessageFormat = useTLSMessageFormat ;
170
- this .localHostName = NetUtils .getLocalHostname ();
169
+ this .localHostName = NetUtils .getCanonicalLocalHostname ();
171
170
ListChecker checker = null ;
172
171
if (excludes != null ) {
173
172
final String [] array = excludes .split (Patterns .COMMA_SEPARATOR );
@@ -642,80 +641,122 @@ public String toString() {
642
641
* @return An Rfc5424Layout.
643
642
* @deprecated Use {@link Rfc5424LayoutBuilder instead}
644
643
*/
645
- @ PluginFactory
644
+ @ Deprecated
646
645
public static Rfc5424Layout createLayout (
647
- // @formatter:off
648
- @ PluginAttribute (value = "facility" , defaultString = "LOCAL0" ) final Facility facility ,
649
- @ PluginAttribute ("id" ) final String id ,
650
- @ PluginAttribute (value = "enterpriseNumber" , defaultInt = DEFAULT_ENTERPRISE_NUMBER )
651
- final int enterpriseNumber ,
652
- @ PluginAttribute (value = "includeMDC" , defaultBoolean = true ) final boolean includeMDC ,
653
- @ PluginAttribute (value = "mdcId" , defaultString = DEFAULT_MDCID ) final String mdcId ,
654
- @ PluginAttribute ("mdcPrefix" ) final String mdcPrefix ,
655
- @ PluginAttribute ("eventPrefix" ) final String eventPrefix ,
656
- @ PluginAttribute (value = "newLine" ) final boolean newLine ,
657
- @ PluginAttribute ("newLineEscape" ) final String escapeNL ,
658
- @ PluginAttribute ("appName" ) final String appName ,
659
- @ PluginAttribute ("messageId" ) final String msgId ,
660
- @ PluginAttribute ("mdcExcludes" ) final String excludes ,
661
- @ PluginAttribute ("mdcIncludes" ) String includes ,
662
- @ PluginAttribute ("mdcRequired" ) final String required ,
663
- @ PluginAttribute ("exceptionPattern" ) final String exceptionPattern ,
664
- // RFC 5425
665
- @ PluginAttribute (value = "useTlsMessageFormat" ) final boolean useTlsMessageFormat ,
666
- @ PluginElement ("LoggerFields" ) final LoggerFields [] loggerFields ,
667
- @ PluginConfiguration final Configuration config ) {
668
- // @formatter:on
646
+ final Facility facility ,
647
+ final String id ,
648
+ final int enterpriseNumber ,
649
+ final boolean includeMDC ,
650
+ final String mdcId ,
651
+ final String mdcPrefix ,
652
+ final String eventPrefix ,
653
+ final boolean newLine ,
654
+ final String escapeNL ,
655
+ final String appName ,
656
+ final String msgId ,
657
+ final String excludes ,
658
+ String includes ,
659
+ final String required ,
660
+ final String exceptionPattern ,
661
+ final boolean useTlsMessageFormat ,
662
+ final LoggerFields [] loggerFields ,
663
+ final Configuration config ) {
669
664
if (includes != null && excludes != null ) {
670
665
LOGGER .error ("mdcIncludes and mdcExcludes are mutually exclusive. Includes wil be ignored" );
671
666
includes = null ;
672
667
}
673
668
674
- return new Rfc5424Layout (
675
- config ,
676
- facility ,
677
- id ,
678
- String .valueOf (enterpriseNumber ),
679
- includeMDC ,
680
- newLine ,
681
- escapeNL ,
682
- mdcId ,
683
- mdcPrefix ,
684
- eventPrefix ,
685
- appName ,
686
- msgId ,
687
- excludes ,
688
- includes ,
689
- required ,
690
- StandardCharsets .UTF_8 ,
691
- exceptionPattern ,
692
- useTlsMessageFormat ,
693
- loggerFields );
694
- }
695
-
696
- public static class Rfc5424LayoutBuilder {
697
- private Configuration config ;
669
+ return newBuilder ()
670
+ .setConfiguration (config )
671
+ .setFacility (facility )
672
+ .setId (id )
673
+ .setEin (String .valueOf (enterpriseNumber ))
674
+ .setIncludeMDC (includeMDC )
675
+ .setIncludeNL (newLine )
676
+ .setEscapeNL (escapeNL )
677
+ .setMdcId (mdcId )
678
+ .setMdcPrefix (mdcPrefix )
679
+ .setEventPrefix (eventPrefix )
680
+ .setAppName (appName )
681
+ .setMessageId (msgId )
682
+ .setExcludes (excludes )
683
+ .setIncludes (includes )
684
+ .setRequired (required )
685
+ .setCharset (StandardCharsets .UTF_8 )
686
+ .setExceptionPattern (exceptionPattern )
687
+ .setUseTLSMessageFormat (useTlsMessageFormat )
688
+ .setLoggerFields (loggerFields )
689
+ .build ();
690
+ }
691
+
692
+ @ PluginBuilderFactory
693
+ public static Rfc5424LayoutBuilder newBuilder () {
694
+ return new Rfc5424LayoutBuilder ();
695
+ }
696
+
697
+ public static class Rfc5424LayoutBuilder extends AbstractStringLayout .Builder <Rfc5424LayoutBuilder >
698
+ implements org .apache .logging .log4j .core .util .Builder <Rfc5424Layout > {
699
+
700
+ @ PluginBuilderAttribute
698
701
private Facility facility = Facility .LOCAL0 ;
702
+
703
+ @ PluginBuilderAttribute
699
704
private String id ;
705
+
706
+ @ PluginBuilderAttribute
700
707
private String ein = String .valueOf (DEFAULT_ENTERPRISE_NUMBER );
708
+
709
+ @ PluginBuilderAttribute
710
+ private Integer enterpriseNumber ;
711
+
712
+ @ PluginBuilderAttribute
701
713
private boolean includeMDC = true ;
714
+
715
+ @ PluginBuilderAttribute
702
716
private boolean includeNL ;
717
+
718
+ @ PluginBuilderAttribute
703
719
private String escapeNL ;
720
+
721
+ @ PluginBuilderAttribute
704
722
private String mdcId = DEFAULT_MDCID ;
723
+
724
+ @ PluginBuilderAttribute
705
725
private String mdcPrefix ;
726
+
727
+ @ PluginBuilderAttribute
706
728
private String eventPrefix ;
729
+
730
+ @ PluginBuilderAttribute
707
731
private String appName ;
732
+
733
+ @ PluginBuilderAttribute
708
734
private String messageId ;
735
+
736
+ @ PluginBuilderAttribute
709
737
private String excludes ;
738
+
739
+ @ PluginBuilderAttribute
710
740
private String includes ;
741
+
742
+ @ PluginBuilderAttribute
711
743
private String required ;
712
- private Charset charset ;
744
+
745
+ @ PluginBuilderAttribute
713
746
private String exceptionPattern ;
747
+
748
+ @ PluginBuilderAttribute
714
749
private boolean useTLSMessageFormat ;
750
+
751
+ @ PluginElement (value = "loggerFields" )
715
752
private LoggerFields [] loggerFields ;
716
753
754
+ /**
755
+ * @deprecated Since 2.24.0 use {@link #setConfiguration} instead.
756
+ */
757
+ @ Deprecated
717
758
public Rfc5424LayoutBuilder setConfig (final Configuration config ) {
718
- this . config = config ;
759
+ setConfiguration ( config ) ;
719
760
return this ;
720
761
}
721
762
@@ -789,9 +830,9 @@ public Rfc5424LayoutBuilder setRequired(final String required) {
789
830
return this ;
790
831
}
791
832
833
+ // Kept for binary compatibility
792
834
public Rfc5424LayoutBuilder setCharset (final Charset charset ) {
793
- this .charset = charset ;
794
- return this ;
835
+ return super .setCharset (charset );
795
836
}
796
837
797
838
public Rfc5424LayoutBuilder setExceptionPattern (final String exceptionPattern ) {
@@ -809,19 +850,24 @@ public Rfc5424LayoutBuilder setLoggerFields(final LoggerFields[] loggerFields) {
809
850
return this ;
810
851
}
811
852
853
+ @ Override
812
854
public Rfc5424Layout build () {
813
855
if (includes != null && excludes != null ) {
814
856
LOGGER .error ("mdcIncludes and mdcExcludes are mutually exclusive. Includes wil be ignored" );
815
857
includes = null ;
816
858
}
817
859
860
+ if (enterpriseNumber != null ) {
861
+ ein = String .valueOf (enterpriseNumber );
862
+ }
818
863
if (ein != null && !ENTERPRISE_ID_PATTERN .matcher (ein ).matches ()) {
819
864
LOGGER .warn (String .format ("provided EID %s is not in valid format!" , ein ));
820
865
return null ;
821
866
}
867
+ final Charset charset = getCharset ();
822
868
823
869
return new Rfc5424Layout (
824
- config ,
870
+ getConfiguration () ,
825
871
facility ,
826
872
id ,
827
873
ein ,
@@ -836,7 +882,7 @@ public Rfc5424Layout build() {
836
882
excludes ,
837
883
includes ,
838
884
required ,
839
- charset ,
885
+ charset != null ? charset : StandardCharsets . UTF_8 ,
840
886
exceptionPattern ,
841
887
useTLSMessageFormat ,
842
888
loggerFields );
@@ -926,4 +972,9 @@ public boolean isIncludeMdc() {
926
972
public String getMdcId () {
927
973
return mdcId ;
928
974
}
975
+
976
+ // Used in tests
977
+ String getLocalHostName () {
978
+ return localHostName ;
979
+ }
929
980
}
0 commit comments