Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/geokureli/logger
Simplify the categorization and prioritization of logs.
https://github.com/geokureli/logger
haxe log logging trace
Last synced: 2 days ago
JSON representation
Simplify the categorization and prioritization of logs.
- Host: GitHub
- URL: https://github.com/geokureli/logger
- Owner: Geokureli
- License: mit
- Created: 2024-12-16T23:19:49.000Z (2 months ago)
- Default Branch: master
- Last Pushed: 2025-01-01T22:22:06.000Z (about 1 month ago)
- Last Synced: 2025-01-01T22:29:40.554Z (about 1 month ago)
- Topics: haxe, log, logging, trace
- Language: Haxe
- Homepage:
- Size: 30.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Logger
Tool used to simplify the categorization of logs, and easily customize which type of logs are displayed, and which throw exceptions.
## Setup
1. Install Logger (coming soon to haxelib, just github for now).
Command Line:
```haxelib git logger http://github.com/Geokureli/Logger```
2. Include Logger in your project.xml:
```xml
```## Creating categories
To create categories, simple instantiate multiple `Logger` instances, like so:```haxe
static public var combatLog = new Logger("Combat", WARN);
static public var resourceLog = new Logger("Res", INFO);
```Typically, though, you would give each static class, tool or important object it's own logger, such as `CombatUtil.log` or `myHero.log`. For uncategorized logs, simply use `Logger.log`
## Logging
To log, you can call Logger instances as if they were functions, for example: `Logger.log(imporantInfo);` or `CombatUtil.log("battle started")`, but each logger also has various priorities that are conditionally logged, like `Logger.log.error("Missing asset")` or `CombatUtil.log.verbose("attacked for 5 damage");`. While these log priorities can be called, they too, also have fields. You can disable a certain priority like so: `log.verbose.enabled = false;` or you can make a certain priority throw exceptions instead of logging via: `log.warn.throws = true;`
Example:
```hx
class Main
{
static public var log = new Logger("Special", WARN, ERROR); // logs warnings, throws exceptions on errors
static public function main():Void
{
log("test log"); // Output: Special - test log
log.warn("test log"); // Output: Special - WARN:test log
log.info("test log"); // ignored
log.setPriority(INFO);
log.info("test log"); // Special - INFO:test log
log.verbose("test log"); // ignored
log.verbose.enabled = true;
log.verbose("test log"); // Output: Special - VERBOSE:test log
try
{
log.error("test log"); // throws exception
}
catch(e)
{
log('exception thrown: ${e.message}'); // Output: Special - exception thrown: Special - ERROR:test log
}
// set custom logger
log.log = (msg, ?pos) -> haxe.Log.trace('SPECIAL_$msg', pos);
log.error.throws = false;
log.error("test log"); // Output: SPECIAL_ERROR:test log
}
}
```## Assertions
Each log priority has an `assert`, which has a variety of standard assertion methods like `isTrue(someBool)` or `equals(someInt, 5)` which will log if untrue. You can also call `assert` directly with a condition and it will output the condition's expression in the logged message. For example:
```hx
final log = Logger.log;
log.error.throws = false;// Check bool
final condition = false;
log.error.assert.isTrue(condition); // Error: Expected true, found falsefinal a = 5;
// check equality
log.error.assert.equals(a, 10); // Error: Expected 10, found 5// check expression
log.error.assert(a == 10); // Error: Assertion failed: a == 10// shorter equivalent
log.assert(a == 10); // Error: Assertion failed: a == 10
```## Enabling logs via compile flags
While the `Logger` constructor has `priority` and `throwPriority` args, these can be overriden from compiler flags, by adding the flag `-D log=WARN` all log priorities less than `WARN` (i.e.: `INFO` and `VERBOSE`) are disabled. You can also specify exactly which priorities are enabled, for example, `-D log=[info,error]` will disable all priorities other than `INFO` and `ERROR`. The `log` flag will also effect all categories, unless the category has it's own log priorities set in compiler flags. For example, a logger with the id "Combat" can have its log priorities set via `-D combat.log=error`. There is a similar `throw` flag to specify which logs throw an exception### Quick overview
- `-D log=WARN`: Set global log-priority to warnings and errors
- `-D throw=ERROR`: Set global throw-priority to errors
- `-D log=[info,error]`: Only log info and errors, not warnings and verbose
- `-D combat.log=verbose`: Enable ALL logs with the id "Combat" (not case sensitive), overrides global log-priority