Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more options to configuration, make dates more consitent #53

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
10 changes: 5 additions & 5 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.6.1"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -56,7 +56,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.3.0-alpha-02"
version: "2.0.0-alpha-01"
fake_async:
dependency: transitive
description:
Expand Down Expand Up @@ -199,7 +199,7 @@ packages:
name: sembast
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0+4"
version: "3.0.3"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -211,7 +211,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -253,7 +253,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
version: "0.3.0"
typed_data:
dependency: transitive
description:
Expand Down
4 changes: 2 additions & 2 deletions lib/data/local/flog_dao.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class FlogDao {
final recordSnapshots = await (_flogsStore.find(
await _db,
finder: finder,
) as FutureOr<List<RecordSnapshot<int, Map<String, Object>>>>);
));

// Making a List<Log> out of List<RecordSnapshot>
return recordSnapshots.map((snapshot) {
Expand All @@ -97,7 +97,7 @@ class FlogDao {
Future<List<Log>> getAllLogs() async {
final recordSnapshots = await (_flogsStore.find(
await _db,
) as FutureOr<List<RecordSnapshot<int, Map<String, Object>>>>);
));

// Making a List<Log> out of List<RecordSnapshot>
return recordSnapshots.map((snapshot) {
Expand Down
78 changes: 44 additions & 34 deletions lib/model/flog/flog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,6 @@ class FLog {
///
/// Returns the default configuration
static LogsConfig getDefaultConfigurations() {
assert(_config != null);
return _config;
}

Expand All @@ -380,16 +379,14 @@ class FLog {
/// @param text the text
/// @param type the type
static void _logThis(
String? className,
String? methodName,
String text,
LogLevel type,
dynamic exception,
String? dataLogType,
StackTrace? stacktrace) {
assert(text != null);
assert(type != null);

String? className,
String? methodName,
String text,
LogLevel type,
dynamic exception,
String? dataLogType,
StackTrace? stacktrace,
) {
//check to see if className is not provided
//then its already been taken from calling class
if (className == null) {
Expand All @@ -402,20 +399,36 @@ class FLog {
methodName = Trace.current().frames[2].member!.split(".")[1];
}

DateTime now = DateTime.now();
//check to see if user provides a valid configuration and logs are enabled
//if not then don't do anything
if (_isLogsConfigValid()) {
final String cnString =
_config.removeNewLines ? className.replaceAll("\n", "") : className;
final String mnString =
_config.removeNewLines ? methodName.replaceAll("\n", "") : methodName;
final String txtString =
_config.removeNewLines ? text.replaceAll("\n", "") : text;
final String? dltString = _config.removeNewLines
? dataLogType?.replaceAll("\n", "")
: dataLogType;
final String exString = _config.removeNewLines
? exception.toString().replaceAll("\n", "")
: exception.toString();
final String stString = _config.removeNewLines
? stacktrace.toString().replaceAll("\n", "")
: stacktrace.toString();
//creating log object
final log = Log(
className: className,
methodName: methodName,
text: text,
className: cnString,
methodName: mnString,
text: txtString,
logLevel: type,
dataLogType: dataLogType,
exception: exception.toString(),
timestamp: DateTimeUtils.getCurrentTimestamp(_config),
timeInMillis: DateTimeUtils.getCurrentTimeInMillis(),
stacktrace: stacktrace.toString(),
dataLogType: dltString,
exception: exString,
timestamp: DateTimeUtils.getCurrentTimestamp(now, _config),
timeInMillis: DateTimeUtils.getCurrentTimeInMillis(now),
stacktrace: stString,
);

//writing it to DB
Expand All @@ -441,7 +454,8 @@ class FLog {
/// _getAllSortedByFilter
///
/// This will return the list of logs sorted by provided filters
static Future<List<Log>> _getAllSortedByFilter({List<Filter>? filters}) async {
static Future<List<Log>> _getAllSortedByFilter(
{List<Filter>? filters}) async {
//check to see if user provides a valid configuration and logs are enabled
//if not then don't do anything
if (_isLogsConfigValid()) {
Expand All @@ -460,20 +474,16 @@ class FLog {
if (_isLogsConfigValid()) {
// skip write logs when log level is to low or
// active log level is not in enabled log levels
if (_config.activeLogLevel != null) {
// skip write logs when log level is to low
if (LogLevel.values.indexOf(_config.activeLogLevel) <=
LogLevel.values.indexOf(log.logLevel!) &&
_config.logLevelsEnabled.contains(_config.activeLogLevel)) {
await _flogDao.insert(log);

//check to see if logcat debugging is enabled
if (_config.isDebuggable) {
print(Formatter.format(log, _config));
}
// skip write logs when log level is to low
if (LogLevel.values.indexOf(_config.activeLogLevel) <=
LogLevel.values.indexOf(log.logLevel!) &&
_config.logLevelsEnabled.contains(_config.activeLogLevel)) {
await _flogDao.insert(log);

//check to see if logcat debugging is enabled
if (_config.isDebuggable) {
print(Formatter.format(log, _config));
}
} else {
throw Exception(Constants.EXCEPTION_NULL_LOGS_LEVEL);
}
} else {
throw Exception(Constants.EXCEPTION_NOT_INIT);
Expand All @@ -485,6 +495,6 @@ class FLog {
/// This will check if user provided any configuration and logs are enabled
/// if yes, then it will return true else it will return false
static _isLogsConfigValid() {
return _config != null && _config.isLogsEnabled;
return _config.isLogsEnabled;
}
}
14 changes: 12 additions & 2 deletions lib/model/flog/flog_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ class LogsConfig {
String encryptionKey = "";

/// Timestamp format
String timestampFormat =
TimestampFormat.TIME_FORMAT_READABLE; //Timestamp format
String timestampFormat = TimestampFormat.TIME_FORMAT_READABLE;

/// If true, print empty field if data is missing. fe. {field1} {field2} {} {null} {field5}
bool printEmpty = false;

/// Display milliseconds since epoch for timestamp field
bool millisecondsSinceEpochForTimestamp = false;

/// If true, removes new line '\n' signs from strings. Usable
/// if you are parsing logs anywhere else, and you need to keep each entry
/// in a single line.
bool removeNewLines = false;
}
15 changes: 4 additions & 11 deletions lib/utils/datetime/date_time.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,16 @@ class DateTimeUtils {
DateTimeUtils._();

//DateTime Methods:-----------------------------------------------------------
static int getCurrentTimeInMillis() {
final now = DateTime.now();
static int getCurrentTimeInMillis(DateTime now) {
return now.millisecondsSinceEpoch;
}

static String getCurrentTimestamp(LogsConfig config) {
final now = DateTime.now();
static String getCurrentTimestamp(DateTime now, LogsConfig config) {
if (config.millisecondsSinceEpochForTimestamp)
return now.millisecondsSinceEpoch.toString();
return DateFormat(config.timestampFormat.toString()).format(now);
}

static String getTimeInMillis(LogsConfig config) {
final now = DateTime.now();
var fiftyDaysFromNow = now.add(Duration(days: -1));
return DateFormat(config.timestampFormat.toString())
.format(fiftyDaysFromNow);
}

static int? getStartAndEndTimestamps({required FilterType type}) {
int? startTimeInMillis;

Expand Down
57 changes: 31 additions & 26 deletions lib/utils/formatter/formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,83 +6,87 @@ class Formatter {
String? output;

if (config.formatType.toString() == FormatType.FORMAT_CURLY.toString()) {
output = _formatCurly(log, config.isDevelopmentDebuggingEnabled);
output = _formatCurly(
log, config.isDevelopmentDebuggingEnabled, config.printEmpty);
} else if (config.formatType.toString() ==
FormatType.FORMAT_SQUARE.toString()) {
output = _formatSquare(log, config.isDevelopmentDebuggingEnabled);
output = _formatSquare(
log, config.isDevelopmentDebuggingEnabled, config.printEmpty);
} else if (config.formatType.toString() ==
FormatType.FORMAT_CSV.toString()) {
output = _formatCsv(
log, config.csvDelimiter, config.isDevelopmentDebuggingEnabled);
output = _formatCsv(log, config.csvDelimiter,
config.isDevelopmentDebuggingEnabled, config.printEmpty);
} else if (config.formatType.toString() ==
FormatType.FORMAT_CUSTOM.toString()) {
output = _formatCustom(
log,
config.customOpeningDivider,
config.customClosingDivider,
config.isDevelopmentDebuggingEnabled,
config.fieldOrderFormatCustom);
config.fieldOrderFormatCustom,
config.printEmpty);
} else {
output = _formatCurly(log, config.isDevelopmentDebuggingEnabled);
output = _formatCurly(
log, config.isDevelopmentDebuggingEnabled, config.printEmpty);
}

return "$output\n";
}

static String? _formatCurly(Log log, bool isDevelopmentDebuggingEnabled) {
static String? _formatCurly(Log log, bool isDevelopmentDebuggingEnabled, bool printEmpty) {
String? output;

output = "{${log.className}} ";
output += "{${log.methodName}} ";
output += "{${log.text}} ";
output += log.exception != 'null' ? "{${log.exception}} " : "";
output += log.exception != 'null' ? "{${log.exception}} " : printEmpty ? "{} " : "";
output += "{${log.logLevel.toString()}} ";
output += "{${log.timestamp}} ";
output += log.stacktrace != 'null' ? "{${log.stacktrace}} " : "";
output += log.stacktrace != 'null' ? "{${log.stacktrace}} " : printEmpty ? "{} " : "";

if (isDevelopmentDebuggingEnabled) {
output += !kReleaseMode ? "{${log.dataLogType}} " : "";
output += !kReleaseMode ? "{${log.timeInMillis}}" : "";
output += !kReleaseMode ? "{${log.dataLogType}} " : printEmpty ? "{} " : "";
output += !kReleaseMode ? "{${log.timeInMillis}}" : printEmpty ? "{} " : "";
}

return output;
}

static String? _formatSquare(Log log, bool isDevelopmentDebuggingEnabled) {
static String? _formatSquare(Log log, bool isDevelopmentDebuggingEnabled, bool printEmpty) {
String? output;

output = "[${log.className}] ";
output += "[${log.methodName}] ";
output += "[${log.text}] ";
output += log.exception != 'null' ? "[${log.exception}] " : "";
output += log.exception != 'null' ? "[${log.exception}] " : printEmpty ? "[] " : "";
output += "[${log.logLevel.toString()}] ";
output += "[${log.timestamp}] ";
output += log.stacktrace != 'null' ? "[${log.stacktrace}] " : "";
output += log.stacktrace != 'null' ? "[${log.stacktrace}] " : printEmpty ? "[] " : "";

if (isDevelopmentDebuggingEnabled) {
output += !kReleaseMode ? "[${log.dataLogType}] " : "";
output += !kReleaseMode ? "[${log.timeInMillis}]" : "";
output += !kReleaseMode ? "[${log.dataLogType}] " : printEmpty ? "[] " : "";
output += !kReleaseMode ? "[${log.timeInMillis}]" : printEmpty ? "[] " : "";
}

return output;
}

static String? _formatCsv(
Log log, String deliminator, bool isDevelopmentDebuggingEnabled) {
Log log, String deliminator, bool isDevelopmentDebuggingEnabled, bool printEmpty) {
String? output;

output = "${log.className}$deliminator ";
output += "${log.methodName}$deliminator ";
output += "${log.text}$deliminator ";
output += log.exception != 'null' ? "${log.exception}$deliminator " : "";
output += log.exception != 'null' ? "${log.exception}$deliminator " : printEmpty ? "$deliminator " : "";
output += "${log.logLevel.toString()}$deliminator ";
output += "${log.timestamp} ";
output +=
log.stacktrace != 'null' ? "${log.stacktrace}$deliminator " : "";
log.stacktrace != 'null' ? "${log.stacktrace}$deliminator " : printEmpty ? "$deliminator " : "";

if (isDevelopmentDebuggingEnabled) {
output += !kReleaseMode ? "${log.dataLogType} " : "";
output += !kReleaseMode ? "${log.timeInMillis}" : "";
output += !kReleaseMode ? "${log.dataLogType} " : printEmpty ? "$deliminator " : "";
output += !kReleaseMode ? "${log.timeInMillis}" : printEmpty ? "$deliminator " : "";
}

return output;
Expand All @@ -94,8 +98,9 @@ class Formatter {
String closingDivider,
bool isDevelopmentDebuggingEnabled,
List<FieldName> fieldOrder,
bool printEmpty,
) {
var output = "";
String output = "";

if (fieldOrder.isNotEmpty) {
fieldOrder.forEach((fieldName) {
Expand All @@ -111,7 +116,7 @@ class Formatter {
if (fieldName == FieldName.EXCEPTION) {
output += log.exception != 'null'
? "$openingDivider${log.exception}$closingDivider "
: "";
: printEmpty ? "$openingDivider$closingDivider " : "";
}
if (fieldName == FieldName.LOG_LEVEL) {
output += "$openingDivider${log.logLevel.toString()}$closingDivider ";
Expand All @@ -122,17 +127,17 @@ class Formatter {
if (fieldName == FieldName.STACKTRACE) {
output += log.stacktrace != 'null'
? "$openingDivider${log.stacktrace}$closingDivider "
: "";
: printEmpty ? "$openingDivider$closingDivider " : "";
}
});

if (isDevelopmentDebuggingEnabled) {
output += !kReleaseMode
? "$openingDivider${log.dataLogType}$closingDivider "
: "";
: printEmpty ? "$openingDivider$closingDivider " : "";
output += !kReleaseMode
? "$openingDivider${log.timeInMillis}$closingDivider"
: "";
: printEmpty ? "$openingDivider$closingDivider " : "";
}
}

Expand Down
Loading