-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
databend-jdbc/src/main/java/com/databend/jdbc/LoggerUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.databend.jdbc; | ||
|
||
import java.io.*; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.stream.Collectors; | ||
|
||
import com.databend.jdbc.log.DatabendLogger; | ||
import com.databend.jdbc.log.JDKLogger; | ||
import com.databend.jdbc.log.SLF4JLogger; | ||
|
||
import lombok.CustomLog; | ||
import lombok.experimental.UtilityClass; | ||
|
||
@UtilityClass | ||
@CustomLog | ||
public class LoggerUtil { | ||
|
||
private static Boolean slf4jAvailable; | ||
|
||
/** | ||
* Provides a {@link DatabendLogger} based on whether SLF4J is available or not. | ||
* | ||
* @param name logger name | ||
* @return a {@link DatabendLogger} | ||
*/ | ||
public static DatabendLogger getLogger(String name) { | ||
if (slf4jAvailable == null) { | ||
slf4jAvailable = isSlf4jJAvailable(); | ||
} | ||
|
||
if (slf4jAvailable) { | ||
return new SLF4JLogger(name); | ||
} else { | ||
return new JDKLogger(name); | ||
} | ||
} | ||
|
||
/** | ||
* Logs the {@link InputStream} | ||
* | ||
* @param is the {@link InputStream} | ||
* @return a copy of the {@link InputStream} provided | ||
*/ | ||
public InputStream logInputStream(InputStream is) { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
try { | ||
byte[] buffer = new byte[1024]; | ||
int len; | ||
while ((len = is.read(buffer)) > -1) { | ||
baos.write(buffer, 0, len); | ||
} | ||
baos.flush(); | ||
InputStream streamToLog = new ByteArrayInputStream(baos.toByteArray()); | ||
String text = new BufferedReader(new InputStreamReader(streamToLog, StandardCharsets.UTF_8)).lines() | ||
.collect(Collectors.joining("\n")); | ||
log.info("======================================"); | ||
log.info(text); | ||
log.info("======================================"); | ||
return new ByteArrayInputStream(baos.toByteArray()); | ||
} catch (Exception ex) { | ||
log.warn("Could not log the stream", ex); | ||
} | ||
return new ByteArrayInputStream(baos.toByteArray()); | ||
} | ||
|
||
private static boolean isSlf4jJAvailable() { | ||
try { | ||
Class.forName("org.slf4j.Logger"); | ||
return true; | ||
} catch (ClassNotFoundException ex) { | ||
return false; | ||
} | ||
} | ||
} |