3434import org .slf4j .Logger ;
3535import org .slf4j .LoggerFactory ;
3636import org .slf4j .event .Level ;
37+ import org .slf4j .spi .LoggingEventBuilder ;
3738
3839import java .io .File ;
3940import java .io .IOException ;
@@ -55,7 +56,7 @@ public record Stickerify(TelegramBot bot, Executor executor) implements UpdatesL
5556 private static final Logger LOGGER = LoggerFactory .getLogger (Stickerify .class );
5657 private static final String BOT_TOKEN = System .getenv ("STICKERIFY_TOKEN" );
5758 private static final ThreadFactory VIRTUAL_THREAD_FACTORY = Thread .ofVirtual ().name ("Virtual-" , 0 ).factory ();
58- public final static ScopedValue <Long > USER = ScopedValue .newInstance ();
59+ public final static ScopedValue <Long > USER_ID = ScopedValue .newInstance ();
5960
6061 /**
6162 * Instantiate the bot processing requests with virtual threads.
@@ -80,7 +81,7 @@ public int process(List<Update> updates) {
8081 updates .forEach (update -> executor .execute (() -> {
8182 if (update .message () != null ) {
8283 var request = new TelegramRequest (update .message ());
83- ScopedValue .where (USER , request .getUserId ()).run (() -> answer (request ));
84+ ScopedValue .where (USER_ID , request .getUserId ()).run (() -> answer (request ));
8485 }
8586 }));
8687
@@ -89,7 +90,7 @@ public int process(List<Update> updates) {
8990
9091 @ Override
9192 public void onException (TelegramException e ) {
92- log (Level .ERROR , "exception_message" , e .getMessage (), "There was an unexpected failure" );
93+ logger (Level .ERROR ). addKeyValue ( "exception_message" , e .getMessage ()). log ( "There was an unexpected failure" );
9394 }
9495
9596 @ Override
@@ -104,7 +105,7 @@ public void close() {
104105 }
105106
106107 private void answer (TelegramRequest request ) {
107- log (Level .INFO , "Received request" );
108+ logger (Level .INFO ). log ( "Received request" );
108109
109110 var file = request .getFile ();
110111
@@ -115,32 +116,8 @@ private void answer(TelegramRequest request) {
115116 }
116117 }
117118
118- private static void log (Level level , String message ) {
119- LOGGER .atLevel (level )
120- .addKeyValue ("user_id" , USER .get ())
121- .log (message );
122- }
123-
124- private static void log (Level level , String message , Throwable cause ) {
125- LOGGER .atLevel (level )
126- .setCause (cause )
127- .addKeyValue ("user_id" , USER .get ())
128- .log (message );
129- }
130-
131- private static void log (Level level , String key , Object value , String message ) {
132- LOGGER .atLevel (level )
133- .addKeyValue ("user_id" , USER .get ())
134- .addKeyValue (key , value )
135- .log (message );
136- }
137-
138- private static void log (Level level , String key , Object value , String message , Throwable cause ) {
139- LOGGER .atLevel (level )
140- .setCause (cause )
141- .addKeyValue ("user_id" , USER .get ())
142- .addKeyValue (key , value )
143- .log (message );
119+ private static LoggingEventBuilder logger (Level level ) {
120+ return LOGGER .atLevel (level ).addKeyValue ("user_id" , USER_ID .get ());
144121 }
145122
146123 private void answerFile (TelegramRequest request , TelegramFile file ) {
@@ -149,7 +126,7 @@ private void answerFile(TelegramRequest request, TelegramFile file) {
149126 } else if (file .canBeDownloaded ()) {
150127 answerFile (request , file .id ());
151128 } else {
152- log (Level .INFO , "Passed-in file is too large" );
129+ logger (Level .INFO ). log ( "Passed-in file is too large" );
153130
154131 answerText (FILE_TOO_LARGE , request );
155132 }
@@ -206,21 +183,21 @@ private void processFailure(TelegramRequest request, BaseException e, String fil
206183 }
207184
208185 if (e instanceof CorruptedFileException ) {
209- log (Level .INFO , "Unable to reply to the request: the file is corrupted" );
186+ logger (Level .INFO ). log ( "Unable to reply to the request: the file is corrupted" );
210187 answerText (CORRUPTED , request );
211188 } else {
212- log (Level .WARN , "file_id" , fileId , "Unable to process file" , e );
189+ logger (Level .WARN ). setCause ( e ). addKeyValue ( "file_id" , fileId ). log ( "Unable to process file" );
213190 answerText (ERROR , request );
214191 }
215192 }
216193
217194 private void processTelegramFailure (TelegramApiException e , boolean logUnmatchedFailure ) {
218195 switch (e .getDescription ()) {
219- case "Bad Request: message to be replied not found" -> log (Level .INFO , "Unable to reply to the request: the message sent has been deleted" );
220- case "Forbidden: bot was blocked by the user" -> log (Level .INFO , "Unable to reply to the request: the user blocked the bot" );
196+ case "Bad Request: message to be replied not found" -> logger (Level .INFO ). log ( "Unable to reply to the request: the message sent has been deleted" );
197+ case "Forbidden: bot was blocked by the user" -> logger (Level .INFO ). log ( "Unable to reply to the request: the user blocked the bot" );
221198 default -> {
222199 if (logUnmatchedFailure ) {
223- log (Level .ERROR , "Unable to reply to the request" , e );
200+ logger (Level .ERROR ). setCause ( e ). log ( "Unable to reply to the request" );
224201 }
225202 }
226203 }
@@ -229,7 +206,7 @@ private void processTelegramFailure(TelegramApiException e, boolean logUnmatched
229206 private void answerText (TelegramRequest request ) {
230207 var message = request .message ();
231208 if (message .text () == null ) {
232- log (Level .INFO , "An unhandled message type has been received" );
209+ logger (Level .INFO ). log ( "An unhandled message type has been received" );
233210 }
234211
235212 answerText (request .getAnswerMessage (), request );
@@ -264,10 +241,10 @@ private static void deleteTempFiles(Set<Path> pathsToDelete) {
264241 for (var path : pathsToDelete ) {
265242 try {
266243 if (!Files .deleteIfExists (path )) {
267- log (Level .INFO , "file_path" , path , "Unable to delete temp file" );
244+ logger (Level .INFO ). addKeyValue ( "file_path" , path ). log ( "Unable to delete temp file" );
268245 }
269246 } catch (IOException e ) {
270- log (Level .ERROR , "file_path" , path , "An error occurred trying to delete temp file" , e );
247+ logger (Level .ERROR ). setCause ( e ). addKeyValue ( "file_path" , path ). log ( "An error occurred trying to delete temp file" );
271248 }
272249 }
273250 }
0 commit comments