2020
2121import org .apache .flink .annotation .PublicEvolving ;
2222import org .apache .flink .configuration .ConfigOption ;
23+ import org .apache .flink .configuration .ConfigOptions ;
24+ import org .apache .flink .configuration .ReadableConfig ;
2325import org .apache .flink .shaded .jackson2 .com .fasterxml .jackson .core .JsonProcessingException ;
2426import org .apache .flink .shaded .jackson2 .com .fasterxml .jackson .databind .ObjectMapper ;
2527import org .apache .flink .streaming .api .functions .sink .SinkFunction ;
2628import org .apache .flink .table .connector .ChangelogMode ;
2729import org .apache .flink .table .connector .sink .DynamicTableSink ;
2830import org .apache .flink .table .connector .sink .SinkFunctionProvider ;
2931import org .apache .flink .table .factories .DynamicTableSinkFactory ;
32+ import org .apache .flink .table .factories .FactoryUtil ;
3033import org .apache .flink .types .RowKind ;
3134import org .slf4j .Logger ;
3235import org .slf4j .LoggerFactory ;
4144@ PublicEvolving
4245public class LoggerTableSinkFactory implements DynamicTableSinkFactory {
4346
44- public static final String IDENTIFIER = "logger" ;
47+ public static final String IDENTIFIER = "logger" ;
48+ public static final ConfigOption <String > PRINT_IDENTIFIER = ConfigOptions
49+ .key ("print-identifier" )
50+ .stringType ()
51+ .defaultValue ("" )
52+ .withDescription ("Message that identify logger and is prefixed to the output of the value." );
4553
4654 @ Override
4755 public String factoryIdentifier () {
@@ -53,17 +61,29 @@ public Set<ConfigOption<?>> requiredOptions() {
5361 return new HashSet <>();
5462 }
5563
56- @ Override
57- public Set <ConfigOption <?>> optionalOptions () {
58- return new HashSet <>();
59- }
64+ @ Override
65+ public Set <ConfigOption <?>> optionalOptions () {
66+ Set <ConfigOption <?>> optionalOptions = new HashSet <>();
67+ optionalOptions .add (PRINT_IDENTIFIER );
68+ return optionalOptions ;
69+ }
6070
61- @ Override
62- public DynamicTableSink createDynamicTableSink (Context context ) {
63- return new LoggerSink ();
64- }
71+ @ Override
72+ public DynamicTableSink createDynamicTableSink (Context context ) {
73+ final FactoryUtil .TableFactoryHelper helper = FactoryUtil .createTableFactoryHelper (this , context );
74+ final ReadableConfig options = helper .getOptions ();
75+ helper .validate ();
6576
66- private static class LoggerSink implements DynamicTableSink {
77+ return new LoggerSink (options .get (PRINT_IDENTIFIER ));
78+ }
79+
80+ private static class LoggerSink implements DynamicTableSink {
81+
82+ private final String printIdentifier ;
83+
84+ public LoggerSink (String printIdentifier ) {
85+ this .printIdentifier = printIdentifier ;
86+ }
6787
6888 @ Override
6989 public ChangelogMode getChangelogMode (ChangelogMode requestedMode ) {
@@ -76,15 +96,15 @@ public ChangelogMode getChangelogMode(ChangelogMode requestedMode) {
7696 return builder .build ();
7797 }
7898
79- @ Override
80- public SinkRuntimeProvider getSinkRuntimeProvider (Context context ) {
81- return SinkFunctionProvider .of (new Slf4jSink <>());
82- }
99+ @ Override
100+ public SinkRuntimeProvider getSinkRuntimeProvider (Context context ) {
101+ return SinkFunctionProvider .of (new Slf4jSink <>(printIdentifier ));
102+ }
83103
84- @ Override
85- public DynamicTableSink copy () {
86- return new LoggerSink ();
87- }
104+ @ Override
105+ public DynamicTableSink copy () {
106+ return new LoggerSink (printIdentifier );
107+ }
88108
89109 @ Override
90110 public String asSummaryString () {
@@ -94,19 +114,29 @@ public String asSummaryString() {
94114}
95115
96116class Slf4jSink <T > implements SinkFunction <T > {
97- private static final Logger LOGGER = LoggerFactory .getLogger (Slf4jSink .class );
98- private static final ObjectMapper MAPPER = new ObjectMapper ();
99-
100- private static final long serialVersionUID = 1L ;
101-
102- @ Override
103- public void invoke (T value ) {
104- String jsonString = "" ;
105- try {
106- jsonString = MAPPER .writeValueAsString (value );
107- } catch (JsonProcessingException e ) {
108- LOGGER .debug ("Unable to serialize value into JSON" , e );
109- }
110- LOGGER .info ("toString: {}, JSON: {}" , value .toString (), jsonString );
111- }
117+ private static final long serialVersionUID = 1L ;
118+
119+ private static final Logger LOGGER = LoggerFactory .getLogger (Slf4jSink .class );
120+ private static final ObjectMapper MAPPER = new ObjectMapper ();
121+ private final String printIdentifier ;
122+
123+ public Slf4jSink (String printIdentifier ) {
124+ this .printIdentifier = printIdentifier ;
125+ }
126+
127+ @ Override
128+ public void invoke (T value , Context context ) {
129+ StringBuilder builder = new StringBuilder ();
130+ builder .append (printIdentifier );
131+
132+ try {
133+ builder .append ("-toString: " );
134+ builder .append (value .toString ());
135+ builder .append (", JSON: " );
136+ builder .append (MAPPER .writeValueAsString (value ));
137+ } catch (JsonProcessingException e ) {
138+ LOGGER .debug ("{}-Unable to serialize value into JSON" , printIdentifier , e );
139+ }
140+ LOGGER .info (builder .toString ());
141+ }
112142}
0 commit comments