Skip to content

Class Jogger

hxs edited this page Dec 1, 2019 · 14 revisions

Jogger

Package jogger
File Jogger.java

Jogger is the principal class to manage the logs.
You can extend me and create your log class. Look JoggerDebug and JoggerError, they are two simple example.


Constructs

Jogger(String... splitLogDir)

This construct set the path to work on, divided by directory.

Parameters:

  • splitLogDir, split path to work on

Example:

// this set the path 'my/path/to/work'
Jogger j = new Jogger("my", "path", "to", "work");

Jogger(String logName, String... splitLogDir)

This construct set the log name and the path.

Parameters:

  • logName, name for log file
  • splitLogDir, split path to work on

Example:

// set only logName and use the default log path
Jogger j = new Jogger("log_name");
// otherwise
Jogger j2 = new Jogger("log_name", "my", "path", "to", "log");

Jogger(String logName, Integer maxSizeBytes, String... splitLogDir)

This construct set the log name, max size of log file and the path.

Parameters:

  • logName, name for log file
  • maxSizeBytes, maximum size of log file, in bytes
  • splitLogDir, split path to work on

Example:

// set only logName and max size, use the default log path
Jogger j = new Jogger("log_name", 1024000);
// otherwise
Jogger j2 = new Jogger("log_name", 1024000, "my", "path", "to", "log");

Methods

Static

void writeLog(String write, String logName, String... splitLogDir)

throws LockLogException

This method write a log.

Parameters:

  • write, string to be writed
  • logName, name for log file
  • splitLogDir, split path to work on

Exceptions:

  • LockLogException, exception thrown if Jogger-Lock failed

Example:

Jogger.writeLog("startng log", "myapp", "my", "dir");

void writeLog(String write, String logName, Integer maxSizeBytes, String... splitLogDir)

throws LockLogException

This method write a log.

Parameters:

  • write, string to be writed
  • logName, name for log file
  • maxSizeBytes, maximum size of log file, in bytes
  • splitLogDir, split path to work on

Exceptions:

  • LockLogException, exception thrown if Jogger-Lock failed

Example:

Jogger.writeLog("startng log", "myapp", 1024000 "my", "dir");

String getLogDirPath(String... splitLogDir)

This method get the path of log directory, by directory list passed.

Return:
Return the string of the path formatted for OS.
The path start with the current working directory, and append to it the folder 'log' with the directory list passed

Parameters:

  • splitLogDir, split path

Example:

String path = Jogger.getLogDirPath("my", "dir", "log");

File getLogFile(String logName, String... splitLogDir)

throws LockLogException

This method get a log file.

Return:
Return the log file to work on.

Parameters:

  • logName, name of log file
  • splitLogDir, split path to search the log file

Exceptions:

  • LockLogException, exception thrown if Jogger-Lock failed

Example:

File file = Jogger.getLogFile("myapp", "my", "dir", "log");

File getLogFile(String logName, Integer maxSizeBytes, String... splitLogDir)

throws LockLogException

This method get a log file.

Return:
Return the log file to work on.

Parameters:

  • logName, name of log file
  • maxSizeBytes, maximum size of log file, in bytes
  • splitLogDir, split path to search the log file

Exceptions:

  • LockLogException, exception thrown if Jogger-Lock failed

Example:

File file = Jogger.getLogFile("myapp", 1024000, "my", "dir", "log");

File getLogFileIfExists(String logName, String... splitLogDir)

throws LockLogException

This method get a log file if it exists.

Return:
If exists return the log file to work on, null otherwise.

Parameters:

  • logName, name of log file
  • splitLogDir, split path to search the log file

Exceptions:

  • LockLogException, exception thrown if Jogger-Lock failed

Example:

File file = Jogger.getLogFileIfExists("myapp", "my", "dir", "log");

File getLogFileIfExists(String logName, Integer maxSizeBytes, String... splitLogDir)

throws LockLogException

This method get a log file if it exists.

Return:
If exists return the log file to work on, null otherwise.

Parameters:

  • logName, name of log file
  • maxSizeBytes, maximum size of log file, in bytes
  • splitLogDir, split path to search the log file

Exceptions:

  • LockLogException, exception thrown if Jogger-Lock failed

Example:

File file = Jogger.getLogFileIfExists("myapp", 1024000, "my", "dir", "log");

String getLogFilePath(String logName, String... splitLogDir)

throws LockLogException

This method get the path of log file.

Return:
Return the path of log file to work on.

Parameters:

  • logName, name of log file
  • splitLogDir, split path to search the log file

Exceptions:

  • LockLogException, exception thrown if Jogger-Lock failed

Example:

String path = Jogger.getLogFilePath("myapp", "my", "dir", "log");

String getLogFilePath(String logName, Integer maxSizeBytes, String... splitLogDir)

throws LockLogException

This method get the path of log file.

Return:
Return the path of log file to work on.

Parameters:

  • logName, name of log file
  • maxSizeBytes, maximum size of log file, in bytes
  • splitLogDir, split path to search the log file

Exceptions:

  • LockLogException, exception thrown if Jogger-Lock failed

Example:

String path = Jogger.getLogFilePath("myapp", 1024000, "my", "dir", "log");

String getLogFilePathIfExists(String logName, String... splitLogDir)

throws LockLogException

This method get the path of log file if it exists.

Return:
If exists return the path of log file to work on, null otherwise.

Parameters:

  • logName, name of log file
  • splitLogDir, split path to search the log file

Exceptions:

  • LockLogException, exception thrown if Jogger-Lock failed

Example:

File file = Jogger.getLogFile("myapp", "my", "dir", "log");

File getLogFileIfExists(String logName, Integer maxSizeBytes, String... splitLogDir)

throws LockLogException

This method get the path of log file if it exists.

Return:
If exists return the path of log file to work on, null otherwise.

Parameters:

  • logName, name of log file
  • maxSizeBytes, maximum size of log file, in bytes
  • splitLogDir, split path to search the log file

Exceptions:

  • LockLogException, exception thrown if Jogger-Lock failed

Example:

File file = Jogger.getLogFile("myapp", 1024000, "my", "dir", "log");

Get and Set

String getPrefixLogFile()

This method get the prefix for log file name.

Default "log_"

Return:
Return the prefix for log file name.

Example:

Jogger jogger = new Jogger();
String string = jogger.getPrefixLogFile();

void setPrefixLogFile(String prefixLogFile)

This method set the prefix for log file name.

Parameters:

  • prefixLogFile, prefix for log file name

Example:

Jogger jogger = new Jogger();
jogger.setPrefixLogFile("mylog_");

String getLogName()

This method get the log name.

Default "jogger"

Return:
Return the log name.

Example:

Jogger jogger = new Jogger();
String string = jogger.getLogName();

void setLogName(String logName)

This method set the log name.

Parameters:

  • logName, log name

Example:

Jogger jogger = new Jogger();
jogger.setLogName("myapp");

String getFileType()

Default ".log"

This method get the log file type.

Return:
Return the log file type.

Example:

Jogger jogger = new Jogger();
String string = jogger.getFileType();

void setFileType(String fileType)

This method set the log file type.

Parameters:

  • fileType, log file type

Example:

Jogger jogger = new Jogger();
jogger.setFileType(".log");

int getMaxSizeBytes()

This method get the maximum size for log file.

Default 51200

Return:
Return the maximum size, in bytes, for log file.

Example:

Jogger jogger = new Jogger();
int maxSize = jogger.getMaxSizeBytes();

void setMaxSizeBytes(int maxSizeBytes)

This method set the maximum size for log file.

Parameters:

  • maxSizeBytes, maximum size, in bytes, for log file

Example:

Jogger jogger = new Jogger();
jogger.setMaxSizeBytes(1024000);

boolean isLock()

This method check if the Jogger-Lock is active.
Read Jogger-Lock.

Default false

Return:
Return true if Jogger-Lock is active, false otherwise.

Example:

Jogger jogger = new Jogger();
jogger.setLock(true);
if (jogger.isLock()) {
  //your code
}

void setLock(boolean lock)

This method can enable or disable Jogger-Lock.
Read Jogger-Lock.

Parameters:

  • lock, true for active it, false otherwise

Example:

Jogger jogger = new Jogger();
jogger.setLock(true);

String[] getSplitLogDir()

This method get the split path of log directory.

Default {"jogger"}

Return:
Return the split path of log directory.

Example:

Jogger jogger = new Jogger();
String[] splitPath = jogger.getSplitLogDir();

void setSplitLogDir(String... splitLogDir)

This method get the split path of log directory.

Parameters:

  • splitLogDir, split path to log directory

Example:

Jogger jogger = new Jogger();
jogger.setSplitLogDir("my", "log", "dir");

Log

void writeLog(String write)

throws LockLogException

This method write the string passed, on the log.
Implement Jogger-Lock. For more info Jogger-Lock.

Parameters:

  • write, string to be writer on log file

Exceptions:

  • LockLogException, exception thrown if Jogger-Lock failed

Example:

Jogger jogger = new Jogger();
jogger.writeLog("YEEEEEAAAAHH!!!");

String getLogFilePath()

throws LogFileException

This method get the path of log file to work on.

Return:
Return the string with the path of log file to work on.

Exceptions:

  • LogFileException, exception thrown if an error is return while working on the log file

Example:

Jogger jogger = new Jogger();
String logFilePath = jogger.getLogFilePath();

File getFile()

throws LogFileException

This method get the log file to work on.

Return:
Return the log file to work on.

Exceptions:

  • LogFileException, exception thrown if an error is return while working on the log file

Example:

Jogger jogger = new Jogger();
File logFile = jogger.getFile();

File getFileIfExists()

throws LogFileException

This method get the log file to work on, only if it exists.

Return:
If the log file exists return it, otherwise return null.

Exceptions:

  • LogFileException, exception thrown if an error is return while working on the log file

Example:

Jogger jogger = new Jogger();
File logFile = jogger.getFileIfExists();

Protected

boolean tryLock()

throws LockLogException

This method is used to implement Jogger-Lock.
It check if Jogger-Lock is enable, then in this case try to lock the file using the class java.util.concurrent.locks.ReentrantLock. For more info Jogger-Lock.

Return:
Return true if Jogger-Lock is disable or the lock is successful, false otherwise.

Exceptions:

  • LockLogException, exception thrown if the timeout for the lock expires

void tryUnlock()

This method is used to implement Jogger-Lock.
It check if Jogger-Lock is enable, then in this case unlock the file using the class java.util.concurrent.locks.ReentrantLock. For more info Jogger-Lock.


Clone this wiki locally