JAVA Record user Action log--write log file __java

Source: Internet
Author: User

Import Java.io.BufferedWriter;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStreamWriter;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;


public class Loginfo {
private static String filepath = "c:\\log\\";
private static Calendar C = Calendar.getinstance ();
Private static final SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
/**
* Log File
* @param file
* @param username
* @param IP
* @param dowhat
*/
public static void Savelog (file file, String username,string ip,string dowhat) {
BufferedWriter bw = NULL;
try {
BW = new BufferedWriter (New OutputStreamWriter (
New FileOutputStream (file, true));
Bw.write ("User" +username+, IP "+ip+", "+dowhat+" Operation \ n ");
Bw.flush ();
catch (FileNotFoundException e) {
SYSTEM.OUT.PRINTLN ("File not Found");
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
finally{
if (NULL!=BW) {
try {
Bw.close ();
catch (IOException e) {
SYSTEM.OUT.PRINTLN ("Flow operation exception");
}
}
}
}

/**
* Create log files based on time
* @return
*/
public static File CreateFile () {
Create a log file every day, named "2010-11-30.txt"
File file = null;
try {
File = new file (Filepath+sdf.format (C.gettime ()) + ". txt");
if (!file.exists ()) {
File.createnewfile ();
}
catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Path does not exist");
}
return file;
}

public static void Main (string[] args) {
File File = CreateFile ();
Savelog (file, "WindForce", "192.168.0.1", "Change Password");
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.