当前位置 : 主页 > 编程语言 > java >

XLog(web项目专用)

来源:互联网 收集:自由互联 发布时间:2022-06-23
package utils; 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.Unkn
package utils;

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.UnknownHostException;

public class XLog {

/**
* log输出到本地文件开关
*/
private static boolean offset = false;
private static String logPath = getHomeDirectory();

public static void showLogArgs(String... args) {
System.out.println(new Throwable().getStackTrace()[1]);
for (int i = 0; i < args.length; i++) {
System.out.println("参数 " + i + " = [" + args[i] + "]");
}
}

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] + "]");
}
}

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] + "]");
}
}


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();
}
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(file, true)));
writer.write(msg + "\n");
writer.flush();
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");
}
}

 


上一篇:CmdUtils
下一篇:没有了
网友评论