forked from Kunagi/ilarkesto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
178 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
/* | ||
* Copyright 2011 Witoslaw Koczewsi <[email protected]>, Artjom Kochtchi | ||
* | ||
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public | ||
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) | ||
* any later version. | ||
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero | ||
* General Public License as published by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the | ||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License | ||
* for more details. | ||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public | ||
* License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/>. | ||
*/ | ||
package ilarkesto.core.logging; | ||
|
||
import ilarkesto.core.base.Str; | ||
import ilarkesto.core.base.Utl; | ||
import ilarkesto.core.logging.Log.Level; | ||
|
||
public class LogRecord { | ||
|
@@ -24,7 +25,7 @@ public class LogRecord { | |
public final Object[] parameters; | ||
public String context; | ||
|
||
public LogRecord(String name, Level level, Object[] parameters) { | ||
public LogRecord(String name, Level level, Object... parameters) { | ||
super(); | ||
this.name = name; | ||
this.level = level; | ||
|
@@ -46,28 +47,51 @@ public String toString() { | |
sb.append(" ").append(nameFormated); | ||
|
||
// text | ||
StringBuilder text = new StringBuilder(); | ||
sb.append(Str.fillUpRight(getParametersAsString(), " ", 100)); | ||
|
||
// context | ||
if (context != null) sb.append(" | ").append(context); | ||
|
||
// extra line for high prio logs | ||
if ((level != Level.DEBUG) && (level != Level.INFO)) sb.append('\n'); | ||
|
||
return sb.toString(); | ||
} | ||
|
||
public String getParametersAsString() { | ||
StringBuilder textSb = new StringBuilder(); | ||
if (parameters == null) { | ||
text.append(" <null>"); | ||
textSb.append(" <null>"); | ||
} else { | ||
for (Object parameter : parameters) { | ||
text.append(' '); | ||
textSb.append(' '); | ||
if (parameter instanceof Throwable) { | ||
text.append("\n").append(Str.getStackTrace((Throwable) parameter)); | ||
textSb.append("\n").append(Str.getStackTrace((Throwable) parameter)); | ||
} else { | ||
text.append(Str.format(parameter)); | ||
textSb.append(Str.format(parameter)); | ||
} | ||
} | ||
} | ||
sb.append(Str.fillUpRight(text.toString(), " ", 100)); | ||
String text = textSb.toString(); | ||
return text; | ||
} | ||
|
||
// context | ||
if (context != null) sb.append(" | ").append(context); | ||
private transient int hashcode; | ||
|
||
// extra line for high prio logs | ||
if ((level != Level.DEBUG) && (level != Level.INFO)) sb.append('\n'); | ||
@Override | ||
public int hashCode() { | ||
if (hashcode == 0) hashcode = Utl.hashCode(level, name, parameters); | ||
return hashcode; | ||
} | ||
|
||
return sb.toString(); | ||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof LogRecord)) return false; | ||
LogRecord other = (LogRecord) obj; | ||
if (level != other.level) return false; | ||
if (!Utl.equals(name, other.name)) return false; | ||
if (!Utl.equals(parameters, other.parameters)) return false; | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2011 Witoslaw Koczewsi <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero | ||
* General Public License as published by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the | ||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public | ||
* License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License along with this program. If not, | ||
* see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package ilarkesto.core.logging; | ||
|
||
import ilarkesto.core.logging.Log.Level; | ||
import ilarkesto.testng.ATest; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
public class LogRecordTest extends ATest { | ||
|
||
@Test | ||
public void hash() { | ||
LogRecord a = new LogRecord("A", Level.INFO, "hello", "logger"); | ||
LogRecord b = new LogRecord("A", Level.INFO, "hello", "logger"); | ||
LogRecord c = new LogRecord("A", Level.INFO, "hello", "world"); | ||
assertEquals(a.hashCode(), b.hashCode()); | ||
assertNotEquals(a.hashCode(), c.hashCode()); | ||
} | ||
|
||
@Test | ||
public void equals() { | ||
LogRecord a = new LogRecord("A", Level.INFO, "hello", "logger"); | ||
LogRecord b = new LogRecord("A", Level.INFO, "hello", "logger"); | ||
LogRecord c = new LogRecord("A", Level.INFO, "hello", "world"); | ||
assertEquals(a, a); | ||
assertEquals(a, b); | ||
assertNotEquals(a, c); | ||
} | ||
|
||
} |