A simple and flexible logging package created for NoCab Transfer.
- Multiple log levels (info, warning, error, fatal)
- Ability to write logs to file
- Support for adding errors and stackTraces to log entries
- Logging between other isolates
Add the following to your pubspec.yaml
file:
dependencies:
nocab_logger:
git: https://github.com/nocab-transfer/nocab-logger.git
import 'package:nocab_logger/nocab_logger.dart';
void main() async {
// If you want to store logs in file, you need to specify the path
var logger = Logger("MainLogger", storeInFile: true, logPath: "path/to/logs/folder");
logger.info("Info message", className: "main");
logger.warning("Warning message", className: "main", error: Exception("test error"), stackTrace: StackTrace.current);
logger.error("Error message", className: "main", error: Exception("test error"), stackTrace: StackTrace.current);
logger.fatal("Fatal message", className: "main", error: Exception("test error"), stackTrace: StackTrace.current);
logger.close();
}
You can fetch the log entries from the file using the getLogs
method:
// If you want to fetch all logs, the logger should be created with storeInFile: true
var logger = Logger("MainLogger", storeInFile: true, logPath: "path/to/logs/folder");
// check if the file is valid
if (await Logger.isFileValid(logger.file!)) {
var logs = await Logger.getLogs(logger.file!);
print(logs);
}
logger.close();
You can also use the Logger.chained
class to log between isolates:
import 'package:nocab_logger/nocab_logger.dart';
void main() async {
var logger = Logger("MainLogger", storeInFile: true, logPath: "path/to/logs/folder");
// Create a new isolate
ReceivePort exitPort = ReceivePort();
await Isolate.spawn(isolate, logger.sendPort, onExit: exitPort.sendPort);
// Listen for logs from the isolate
logger.onLog.listen((log) {
print(log);
});
// Wait for the isolate to finish
await exitPort.first;
logger.close();
}
void isolate(SendPort mainLoggerSendPort) {
var logger = Logger.chained(mainLoggerSendPort);
logger.info("Info message", className: "isolate");
logger.warning("Warning message", className: "isolate", error: Exception("test error"), stackTrace: StackTrace.current);
}
[INFO] - 2023-02-27T20:01:27.946352 MainLogger.main: Info message
[WARNING] - 2023-02-27T20:01:27.946352 MainLogger.main: Warning message [Exception: test error] [StackTrace: #0 main (file:///Users/username/Projects/nocab-logger/example/main.dart:10:5)]
[ERROR] - 2023-02-27T20:01:27.946352 MainLogger.main: Error message [Exception: test error] [StackTrace: #0 main (file:///Users/username/Projects/nocab-logger/example/main.dart:11:5)]
[FATAL] - 2023-02-27T20:01:27.946352 MainLogger.main: Fatal message [Exception: test error] [StackTrace: #0 main (file:///Users/username/Projects/nocab-logger/example/main.dart:12:5)]
- Fork it
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create new Pull Request
This project is licensed under the MIT License - see the LICENSE file for details