From 19ddc8114c12da008a45aee980657d08f40574be Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Wed, 2 Jun 2021 17:08:41 +0700 Subject: [PATCH] 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 --- example/cubetiq_xlog_example.dart | 33 ++++++++++++ lib/configurable.dart | 2 +- lib/print_color.dart | 3 ++ lib/src/print/print_color.dart | 55 ++++++++++++++++++++ lib/src/util/datetime_functions.dart | 2 + lib/src/xlog/simple_xlog_provider.dart | 37 ++++++++++++++ lib/src/xlog/xlog.dart | 30 +++++++++++ lib/src/xlog/xlog_factory.dart | 12 +++++ lib/src/xlog/xlog_provider.dart | 69 ++++++++++++++++++++++++++ lib/text.dart | 2 +- lib/utils.dart | 3 ++ lib/xlog.dart | 6 +++ pubspec.yaml | 3 ++ 13 files changed, 255 insertions(+), 2 deletions(-) create mode 100644 example/cubetiq_xlog_example.dart create mode 100644 lib/print_color.dart create mode 100644 lib/src/print/print_color.dart create mode 100644 lib/src/util/datetime_functions.dart create mode 100644 lib/src/xlog/simple_xlog_provider.dart create mode 100644 lib/src/xlog/xlog.dart create mode 100644 lib/src/xlog/xlog_factory.dart create mode 100644 lib/src/xlog/xlog_provider.dart create mode 100644 lib/utils.dart create mode 100644 lib/xlog.dart diff --git a/example/cubetiq_xlog_example.dart b/example/cubetiq_xlog_example.dart new file mode 100644 index 0000000..f898ea5 --- /dev/null +++ b/example/cubetiq_xlog_example.dart @@ -0,0 +1,33 @@ +import 'package:cubetiq/xlog.dart'; + +void main(List 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, + }; +} diff --git a/lib/configurable.dart b/lib/configurable.dart index 9d56d6e..63d6f05 100644 --- a/lib/configurable.dart +++ b/lib/configurable.dart @@ -1,4 +1,4 @@ -library cubetiq; +library configurable; export 'src/configurable/configurable.dart'; export 'src/configurable/configuration_provider.dart'; diff --git a/lib/print_color.dart b/lib/print_color.dart new file mode 100644 index 0000000..ba15219 --- /dev/null +++ b/lib/print_color.dart @@ -0,0 +1,3 @@ +library print_color; + +export 'src/print/print_color.dart'; diff --git a/lib/src/print/print_color.dart b/lib/src/print/print_color.dart new file mode 100644 index 0000000..2cda380 --- /dev/null +++ b/lib/src/print/print_color.dart @@ -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()))); + } +} diff --git a/lib/src/util/datetime_functions.dart b/lib/src/util/datetime_functions.dart new file mode 100644 index 0000000..fdad4ba --- /dev/null +++ b/lib/src/util/datetime_functions.dart @@ -0,0 +1,2 @@ +DateTime now() => DateTime.now(); +String nowToString() => now().toString(); diff --git a/lib/src/xlog/simple_xlog_provider.dart b/lib/src/xlog/simple_xlog_provider.dart new file mode 100644 index 0000000..9484f15 --- /dev/null +++ b/lib/src/xlog/simple_xlog_provider.dart @@ -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); + } +} diff --git a/lib/src/xlog/xlog.dart b/lib/src/xlog/xlog.dart new file mode 100644 index 0000000..747cdf0 --- /dev/null +++ b/lib/src/xlog/xlog.dart @@ -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); + } +} diff --git a/lib/src/xlog/xlog_factory.dart b/lib/src/xlog/xlog_factory.dart new file mode 100644 index 0000000..8a5500f --- /dev/null +++ b/lib/src/xlog/xlog_factory.dart @@ -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; + } +} diff --git a/lib/src/xlog/xlog_provider.dart b/lib/src/xlog/xlog_provider.dart new file mode 100644 index 0000000..60bfba7 --- /dev/null +++ b/lib/src/xlog/xlog_provider.dart @@ -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]); +} diff --git a/lib/text.dart b/lib/text.dart index 49fd584..d6ced1a 100644 --- a/lib/text.dart +++ b/lib/text.dart @@ -1,4 +1,4 @@ -library cubetiq; +library text; export 'src/text/text_formatter.dart'; export 'src/text/extension.dart'; diff --git a/lib/utils.dart b/lib/utils.dart new file mode 100644 index 0000000..69fdb2f --- /dev/null +++ b/lib/utils.dart @@ -0,0 +1,3 @@ +library utils; + +export 'src/util/datetime_functions.dart'; diff --git a/lib/xlog.dart b/lib/xlog.dart new file mode 100644 index 0000000..db9127c --- /dev/null +++ b/lib/xlog.dart @@ -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'; diff --git a/pubspec.yaml b/pubspec.yaml index 3f5f4f9..0394d78 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,6 +7,9 @@ repository: https://git.cubetiqs.com/CUBETIQ/cubetiq_dart_shared.git environment: sdk: '>=2.12.0 <3.0.0' +dependencies: + ansicolor: ^2.0.1 + dev_dependencies: pedantic: ^1.10.0 test: ^1.16.0