Task: Add XLog and factory logger with simple implemented with print and add functions more for utils and print color that using ansi color lib and export some useful methods and functions

This commit is contained in:
Sambo Chea 2021-06-02 17:08:41 +07:00
parent 881aaae3ad
commit 19ddc8114c
13 changed files with 255 additions and 2 deletions

View File

@ -0,0 +1,33 @@
import 'package:cubetiq/xlog.dart';
void main(List<String> args) {
XLog.error('My some error here {0} and me {1}', ['Sambo', 'Chea']);
XLog.debug('My some error here {0} and me {1}', ['Sambo', 'Chea']);
XLog.info('My some error here {0} and me {1}', ['Sambo', 'Chea']);
XLog.success('My some error here {0} and me {1}', ['Sambo', 'Chea']);
XLog.trace('My some error here {0} and me {1}', ['Sambo', 'Chea']);
XLog.warning('My some error here {0} and me {1}', ['Sambo', 'Chea']);
XLog.warning(null);
var json = Person(1, 'Sambo');
XLog.warning(json, [null, null]);
}
class Person {
final id;
final name;
Person(this.id, this.name);
Map toJson() => {
'id': id,
'name': name,
};
}

View File

@ -1,4 +1,4 @@
library cubetiq; library configurable;
export 'src/configurable/configurable.dart'; export 'src/configurable/configurable.dart';
export 'src/configurable/configuration_provider.dart'; export 'src/configurable/configuration_provider.dart';

3
lib/print_color.dart Normal file
View File

@ -0,0 +1,3 @@
library print_color;
export 'src/print/print_color.dart';

View File

@ -0,0 +1,55 @@
import 'package:ansicolor/ansicolor.dart';
AnsiPen _green = AnsiPen()..green();
AnsiPen _red = AnsiPen()..red();
AnsiPen _white = AnsiPen()..white();
AnsiPen _yellow = AnsiPen()..yellow();
AnsiPen _gray = AnsiPen()..gray();
AnsiPen _magenta = AnsiPen()..magenta();
AnsiPen _cyan = AnsiPen()..cyan();
class PrintColor {
static final _pattern = RegExp('.{1,800}');
static void white(text) {
return _pattern
.allMatches(text)
.forEach((match) => print(_white(match.group(0).toString())));
}
static void green(text) {
return _pattern
.allMatches(text)
.forEach((match) => print(_green(match.group(0).toString())));
}
static void red(text) {
return _pattern
.allMatches(text)
.forEach((match) => print(_red(match.group(0).toString())));
}
static void yellow(text) {
return _pattern
.allMatches(text)
.forEach((match) => print(_yellow(match.group(0).toString())));
}
static void gray(text) {
return _pattern
.allMatches(text)
.forEach((match) => print(_gray(match.group(0).toString())));
}
static void magenta(text) {
return _pattern
.allMatches(text)
.forEach((match) => print(_magenta(match.group(0).toString())));
}
static void cyan(text) {
return _pattern
.allMatches(text)
.forEach((match) => print(_cyan(match.group(0).toString())));
}
}

View File

@ -0,0 +1,2 @@
DateTime now() => DateTime.now();
String nowToString() => now().toString();

View File

@ -0,0 +1,37 @@
import 'xlog_provider.dart';
class SimpleXLogProvider with XLogProvider {
final String? _prefix;
SimpleXLogProvider(this._prefix);
@override
void debug(data, [List? args]) {
show(XLogType.DEBUG, _prefix, data, args);
}
@override
void error(data, [List? args]) {
show(XLogType.ERROR, _prefix, data, args);
}
@override
void info(data, [List? args]) {
show(XLogType.INFO, _prefix, data, args);
}
@override
void success(data, [List? args]) {
show(XLogType.SUCCESS, _prefix, data, args);
}
@override
void trace(data, [List? args]) {
show(XLogType.TRACE, _prefix, data, args);
}
@override
void warning(data, [List? args]) {
show(XLogType.WARNING, _prefix, data, args);
}
}

30
lib/src/xlog/xlog.dart Normal file
View File

@ -0,0 +1,30 @@
import 'xlog_factory.dart';
import 'xlog_provider.dart';
class XLog {
static XLogProvider logger = XLogFactory.getSimpleLogger('XLOG');
static void success(dynamic data, [List? args]) {
logger.success(data, args);
}
static void info(dynamic data, [List? args]) {
logger.info(data, args);
}
static void warning(dynamic data, [List? args]) {
logger.warning(data, args);
}
static void error(dynamic data, [List? args]) {
logger.error(data, args);
}
static void debug(dynamic data, [List? args]) {
logger.debug(data, args);
}
static void trace(dynamic data, [List? args]) {
logger.trace(data, args);
}
}

View File

@ -0,0 +1,12 @@
import 'simple_xlog_provider.dart';
import 'xlog_provider.dart';
class XLogFactory {
static XLogProvider getSimpleLogger(String? prefix) {
return SimpleXLogProvider(prefix);
}
static XLogProvider getLogger(XLogProvider provider) {
return provider;
}
}

View File

@ -0,0 +1,69 @@
import 'dart:convert';
import 'package:cubetiq/src/print/print_color.dart';
import 'package:cubetiq/text.dart';
import 'package:cubetiq/utils.dart';
enum XLogType { SUCCESS, INFO, ERROR, WARNING, DEBUG, TRACE }
abstract class XLogProvider {
bool isEnabled(XLogType type) {
return true;
}
/// Example Format
/// [TYPE] 2021-01-31 12:30:00.0000: PREFIX => DATA
void show(XLogType type, String? prefix, dynamic data, [List? args]) {
if (!isEnabled(type)) {
return;
}
prefix ??= '';
data = jsonEncode(data);
var content = '';
if (args == null || args.isEmpty) {
content = data;
} else {
content = StringUtils.textFormat(data, args) ?? 'null';
}
var text = '[$type] ${nowToString()}: $prefix => $content'.trim();
switch (type) {
case XLogType.SUCCESS:
PrintColor.green(text);
break;
case XLogType.INFO:
PrintColor.cyan(text);
break;
case XLogType.WARNING:
PrintColor.yellow(text);
break;
case XLogType.ERROR:
PrintColor.red(text);
break;
case XLogType.DEBUG:
PrintColor.gray(text);
break;
case XLogType.TRACE:
PrintColor.magenta(text);
break;
default:
print(text);
break;
}
}
void success(dynamic data, [List? args]);
void info(dynamic data, [List? args]);
void warning(dynamic data, [List? args]);
void debug(dynamic data, [List? args]);
void error(dynamic data, [List? args]);
void trace(dynamic data, [List? args]);
}

View File

@ -1,4 +1,4 @@
library cubetiq; library text;
export 'src/text/text_formatter.dart'; export 'src/text/text_formatter.dart';
export 'src/text/extension.dart'; export 'src/text/extension.dart';

3
lib/utils.dart Normal file
View File

@ -0,0 +1,3 @@
library utils;
export 'src/util/datetime_functions.dart';

6
lib/xlog.dart Normal file
View File

@ -0,0 +1,6 @@
library xlog;
export 'src/xlog/xlog.dart';
export 'src/xlog/xlog_provider.dart';
export 'src/xlog/simple_xlog_provider.dart';
export 'src/xlog/xlog_factory.dart';

View File

@ -7,6 +7,9 @@ repository: https://git.cubetiqs.com/CUBETIQ/cubetiq_dart_shared.git
environment: environment:
sdk: '>=2.12.0 <3.0.0' sdk: '>=2.12.0 <3.0.0'
dependencies:
ansicolor: ^2.0.1
dev_dependencies: dev_dependencies:
pedantic: ^1.10.0 pedantic: ^1.10.0
test: ^1.16.0 test: ^1.16.0