Skip to content

Commit 0b2dd41

Browse files
author
Rhisiart ap Gwilym
committed
Initial commit
0 parents  commit 0b2dd41

13 files changed

+645
-0
lines changed

.github/PULL_REQUEST_TEMPLATE.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!--
2+
Thanks for contributing!
3+
4+
Provide a description of your changes below and a general summary in the title
5+
6+
Please look at the following checklist to ensure that your PR can be accepted quickly:
7+
-->
8+
9+
## Description
10+
11+
<!--- Describe your changes in detail -->
12+
13+
## Type of Change
14+
15+
<!--- Put an `x` in all the boxes that apply: -->
16+
17+
- [ ] ✨ New feature (non-breaking change which adds functionality)
18+
- [ ] 🛠️ Bug fix (non-breaking change which fixes an issue)
19+
- [ ] ❌ Breaking change (fix or feature that would cause existing functionality to change)
20+
- [ ] 🧹 Code refactor
21+
- [ ] ✅ Build configuration change
22+
- [ ] 📝 Documentation
23+
- [ ] 🗑️ Chore

.github/workflows/main.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: ci
2+
on:
3+
pull_request:
4+
push:
5+
branches:
6+
# should not be possible to push to dev/master once branch permissions are set
7+
- dev
8+
- master
9+
jobs:
10+
validate:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- uses: subosito/flutter-action@v2
15+
with:
16+
channel: stable
17+
- run: flutter --version
18+
- run: ./ci_script.sh
19+
shell: bash

.gitignore

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
12+
# IntelliJ related
13+
*.iml
14+
*.ipr
15+
*.iws
16+
.idea/
17+
18+
# VSCode related
19+
.vscode/
20+
21+
# Flutter/Dart/Pub related
22+
**/doc/api/
23+
**/ios/Flutter/.last_build_id
24+
.dart_tool/
25+
.flutter-plugins
26+
.flutter-plugins-dependencies
27+
.packages
28+
.pub-cache/
29+
.pub/
30+
/build/
31+
32+
# Web related
33+
lib/generated_plugin_registrant.dart
34+
35+
# Symbolication related
36+
app.*.symbols
37+
38+
# Obfuscation related
39+
app.*.map.json

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# logger_service
2+
3+
[![style: very good analysis][very_good_analysis_badge]][very_good_analysis_link]
4+
5+
A log service that wraps an implementation to provide a common interface
6+
7+
---
8+
9+
## Lint, format, test, and enforce code coverage 🧪
10+
11+
To help reduce broken CI builds, please run the following command from from your project's root directory, which will ensure that any custom git hooks are actioned before being pushed to the remote repository:
12+
13+
```sh
14+
git config --local core.hooksPath hooks
15+
```
16+
17+
18+
[license_badge]: https://img.shields.io/badge/license-MIT-blue.svg
19+
[license_link]: https://opensource.org/licenses/MIT
20+
[very_good_analysis_badge]: https://img.shields.io/badge/style-very_good_analysis-B22C89.svg
21+
[very_good_analysis_link]: https://pub.dev/packages/very_good_analysis

analysis_options.yaml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
include: package:very_good_analysis/analysis_options.yaml
2+
3+
dart_code_metrics:
4+
anti-patterns:
5+
- long-method
6+
- long-parameter-list
7+
metrics:
8+
cyclomatic-complexity: 20
9+
maximum-nesting-level: 5
10+
number-of-parameters: 4
11+
source-lines-of-code: 50
12+
metrics-exclude:
13+
- test/**
14+
rules:
15+
- newline-before-return
16+
- no-boolean-literal-compare
17+
- no-empty-block
18+
- prefer-trailing-comma
19+
- prefer-conditional-expressions
20+
- no-equal-then-else

ci_script.sh

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
set -e # exit on first error
3+
4+
###############################################################################
5+
# VERIFICATION FUNCTION FOR FLUTTER APP/PACKAGES
6+
###############################################################################
7+
verifyFlutter () {
8+
cd $1
9+
echo "Checking `pwd`"
10+
11+
flutter clean
12+
flutter pub get
13+
flutter format --set-exit-if-changed .
14+
flutter pub run dart_code_metrics:metrics analyze . --fatal-style --fatal-performance --fatal-warnings
15+
flutter pub run dart_code_metrics:metrics check-unused-files . --fatal-unused
16+
flutter pub run dart_code_metrics:metrics check-unused-l10n . --fatal-unused
17+
flutter pub run dart_code_metrics:metrics check-unused-code . --fatal-unused
18+
flutter analyze --no-pub .
19+
flutter test
20+
21+
cd -
22+
}
23+
24+
###############################################################################
25+
# INVOKE VERIFICATION FUNCTIONS
26+
###############################################################################
27+
verifyFlutter "."

hooks/pre-push

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
set -e # exit on first error
3+
4+
./ci_script.sh

lib/logger_service.dart

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
library logger_service;
2+
3+
export 'src/logger_service.dart';

lib/src/custom_pretty_printer.dart

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import 'package:logger/logger.dart';
2+
3+
/// Customer [PrettyPrinter] that provides the capability to filter
4+
/// the stacktrace
5+
class CustomPrettyPrinter extends PrettyPrinter {
6+
/// See [LogPrinter]
7+
CustomPrettyPrinter({
8+
super.stackTraceBeginIndex,
9+
super.methodCount,
10+
super.errorMethodCount,
11+
super.lineLength,
12+
super.colors,
13+
super.printEmojis,
14+
super.printTime,
15+
super.excludeBox,
16+
super.noBoxingByDefault,
17+
this.stacktraceFilters = const [],
18+
});
19+
20+
/// Property to allow the filtering of stacktraces
21+
final List<RegExp> stacktraceFilters;
22+
23+
@override
24+
bool includeStacktraceLine(String line) =>
25+
super.includeStacktraceLine(line) && !_discardUserStacktraceLine(line);
26+
27+
bool _discardUserStacktraceLine(String line) =>
28+
stacktraceFilters.any((element) => element.hasMatch(line));
29+
}

lib/src/logger_service.dart

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import 'dart:io';
2+
3+
import 'package:flutter/foundation.dart';
4+
import 'package:logger/logger.dart';
5+
import 'package:logger_service/src/custom_pretty_printer.dart';
6+
7+
/// Class that wraps https://pub.dev/packages/logger for a common
8+
/// interface should we decide to change implementations in future
9+
class Log {
10+
/// Creates and configures the underlying logger implementation
11+
factory Log() {
12+
if (kDebugMode) {
13+
_logger ??= Logger(
14+
printer: printer ??= SimplePrinter(colors: _isColorEnabled),
15+
);
16+
}
17+
18+
return _instance;
19+
}
20+
21+
Log._internal();
22+
23+
/// Enable/disable logging - useful for suppressing logging during unit tests
24+
static bool enabled = true;
25+
26+
static bool get _isColorEnabled => !kIsWeb && !Platform.isIOS;
27+
28+
static final Log _instance = Log._internal();
29+
30+
static Logger? _logger;
31+
32+
/// Default tag - replace with your own if required
33+
static String tag = 'default';
34+
35+
/// Default stacktrace filter - add/replace with your own if required
36+
static List<RegExp> stacktraceFilters = [
37+
RegExp('^(.*logger_service.dart).*'),
38+
RegExp('^(.*<asynchronous suspension>).*'),
39+
];
40+
41+
/// Default printer - uses [SimplePrinter] if null
42+
static LogPrinter? printer = CustomPrettyPrinter(
43+
colors: _isColorEnabled,
44+
methodCount: 3,
45+
stacktraceFilters: stacktraceFilters,
46+
);
47+
48+
/// Log a message at level [Level.verbose].
49+
void v(dynamic message, [dynamic error, StackTrace? stackTrace]) {
50+
Logger.level = Level.warning;
51+
52+
_log(Level.verbose, message, error, stackTrace);
53+
}
54+
55+
/// Log a message at level [Level.debug].
56+
void d(dynamic message, [dynamic error, StackTrace? stackTrace]) {
57+
_log(Level.debug, message, error, stackTrace);
58+
}
59+
60+
/// Log a message at level [Level.info].
61+
void i(dynamic message, [dynamic error, StackTrace? stackTrace]) {
62+
_log(Level.info, message, error, stackTrace);
63+
}
64+
65+
/// Log a message at level [Level.warning].
66+
void w(dynamic message, [dynamic error, StackTrace? stackTrace]) {
67+
_log(Level.warning, message, error, stackTrace);
68+
}
69+
70+
/// Log a message at level [Level.error].
71+
void e(dynamic message, [dynamic error, StackTrace? stackTrace]) {
72+
_log(Level.error, message, error, stackTrace);
73+
}
74+
75+
/// Log a message at level [Level.wtf].
76+
void wtf(dynamic message, [dynamic error, StackTrace? stackTrace]) {
77+
_log(Level.wtf, message, error, stackTrace);
78+
}
79+
80+
void _log(
81+
Level level,
82+
dynamic message, [
83+
dynamic error,
84+
StackTrace? stackTrace,
85+
]) {
86+
if (enabled) {
87+
// see https://github.com/leisim/logger/issues/98
88+
// ignore: parameter_assignments
89+
if (stackTrace != null) error = '$error\n${stackTrace.toString()}';
90+
91+
_logger?.log(level, '$tag: $message', error, stackTrace);
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)