import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StringWriter; import java.net.UnknownHostExceptio
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.UnknownHostException;
public class XLog {
/** log输出到本地文件开关 */
private static boolean offset = true;
private static String logPath = getHomeDirectory();
public static void showLogArgs(String... args) {
System.out.println(new Throwable().getStackTrace()[1]);
StringBuilder argsS = new StringBuilder();
for (int i = 0; i < args.length; i++) {
argsS.append("参数 ").append(i).append(" = [").append(args[i]).append("]");
}
System.out.println(argsS.toString());
file(argsS.toString(), "普通信息.txt");
}
public static void getChild(String... args) {
System.out.println(new Throwable().getStackTrace()[2]);
for (int i = 0; i < args.length; i++) {
System.out.println("参数 " + i + " = [" + args[i] + "]");
file(args[i], "普通信息.txt");
}
}
public static void getGrandson(String... args) {
System.out.println(new Throwable().getStackTrace()[1]);
System.out.println(new Throwable().getStackTrace()[2]);
System.out.println(new Throwable().getStackTrace()[3]);
System.out.println(new Throwable().getStackTrace()[4]);
System.out.println(new Throwable().getStackTrace()[5]);
System.out.println(new Throwable().getStackTrace()[6]);
System.out.println(new Throwable().getStackTrace()[7]);
System.out.println(new Throwable().getStackTrace()[8]);
for (int i = 0; i < args.length; i++) {
System.out.println("参数 " + i + " = [" + args[i] + "]");
file(args[i], "普通信息.txt");
}
}
public static void showExceptionLogInfo(Exception msg) {
String detilErrMsg = " " + msg + "\n详情: " + new Throwable().getStackTrace()[1] + "";
System.out.println("报错信息:" + detilErrMsg);
file(getStackTraceString(msg) + "\n详细信息:\n" + detilErrMsg, "异常信息.txt");
}
public static void showLogInfo(String tag, Object msg) {
String commonMsg = new Throwable().getStackTrace()[1] + "\n详情: " + tag + ": " + msg;
System.out.println("调用位置: " + commonMsg);
file(msg.toString(), "普通信息.txt");
}
public static void showLogInfo(String msg) {
String commonMsg = new Throwable().getStackTrace()[1] + "\n详情: " + msg + "";
System.out.println("调用位置: " + commonMsg);
file(msg, "普通信息.txt");
}
// /**
// * 纯写文件
// */
// public static void file(String msg) {
// try {
// writeToFile(msg, getHomeDirectory() + "日志信息.log");
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
/**
* @param msg 日志信息
* @param logFileName 日志文件名称
* @throws IOException IOException
*/
private static void file(String msg, String logFileName) {
if (!offset) {
return;
}
if (logPath == null) {
logPath = getHomeDirectory();
}
try {
File file = new File(logPath + logFileName);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file, true);
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(out, StandardCharsets.UTF_8));
writer.write(msg + "\n");
writer.flush();
out.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getStackTraceString(Throwable tr) {
if (tr == null) {
return "";
}
// This is to reduce the amount of log spew that apps do in the non-error
// condition of the network being unavailable.
Throwable t = tr;
while (t != null) {
if (t instanceof UnknownHostException) {
return "";
}
t = t.getCause();
}
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, false);
tr.printStackTrace(pw);
pw.flush();
return sw.toString();
}
/**
* @return windows桌面路径
*/
private static String getHomeDirectory() {
javax.swing.filechooser.FileSystemView fsv = javax.swing.filechooser.FileSystemView
.getFileSystemView();
// System.out.println("桌面路径:" + fsv.getHomeDirectory());
// System.out.println("默认路径:" + fsv.getDefaultDirectory());
// System.getProperties().list(System.out);
return fsv.getHomeDirectory().getPath() + File.separator;
}
public static void main(String[] args) {
file("测试信息", "测试.log");
}
}
控制台点击可以跳转