Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hantmac committed Oct 19, 2023
1 parent 8acd3d0 commit 9c5375e
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions databend-jdbc/src/main/java/com/databend/jdbc/LoggerUtil.java
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;
}
}
}

0 comments on commit 9c5375e

Please sign in to comment.