SourceHorizon / logger

Small, easy to use and extensible logger which prints beautiful logs.
https://pub.dev/packages/logger
MIT License
197 stars 33 forks source link

Unable to use FileOutput() #62

Closed ArchitMukherjee closed 6 months ago

ArchitMukherjee commented 7 months ago

I am trying to make a logger that would save the logs to a file in a specified directory. But I am unable to get the file for saving the logs. My logging.dart file is

import 'dart:io';
import 'package:logger/logger.dart';
// import 'package:path_provider/path_provider.dart';

File logFile = File('logs.txt');

final logger = Logger(
  printer: PrettyPrinter(),
  level: Level.all,
  output: FileOutput(file: logFile),
);

I tried to use the following code as given in #43

import 'dart:io';
import 'package:logger/logger.dart';
import 'package:path_provider/path_provider.dart';

late File logFile;

Directory applicationDir = await getApplicationDocumentsDirectory();
logFile = File("${applicationDir.path}${Platform.pathSeparator}test.log");

// ...

static final Logger log = Logger(
    printer: PrettyPrinter(),
    output: FileOutput(file: logFile),
);

but it is showing giving errors when I use the await keyword.

What I want to do is import this file wherever required and use the logger variable to log information. Is this the right approach or should I do something else? Also please provide an example for logging out to a file as asked in #56

Bungeefan commented 6 months ago

Hi, this seems to be more of a code question than a library issue. In other words, please ask such questions on StackOverflow or similar in the future.

Nevertheless, I guess this approach seems to be feasible, however, I can't really recommend anything without knowing your problem/use-case.

Also, the provided code wasn't intended to be a copy-paste snippet, but more of an "only the important bits are included" snippet. As for your error, await can only be used in an async method, like in:

void main() async {
  // ...
}

And regarding the request for an example, please only announce your support in the respective issue, by either upvoting the initial issue comment or posting a comment yourself.