Running `.dart` files does not show logging messages - dart

I just cannot figure out how to show logging messages when running dart files from terminal.
Example example.dart:
import 'dart:developer';
void main() {
print('print');
log('log');
}
Expected output:
print
log
Actual output:
print
I tried calling dart example.dart, dart run --all example.dart, dart run --verbosity=all example.dart, and different values instead of all (info, ...).
But non of these produced any helpful error messages let alone the out print I expect.

APIs from dart:developer (such as log) are intended to interact with debugging tools:
Interact with developer tools such as the debugger and inspector.
It's not explicit from the documentation for log, but I'd expect it to send a log message only to an attached debugger, not to the console.
If you want logging output that is independent of a debugger, use package:logging and add a listener that calls print, as shown by the example.

Related

What are the actual differences between the print() vs debugPrint() and log() in Flutter?

I am trying to create a util for logging.
I need to know the actual differences between print() vs debugPrint() and log().
Many answers I have seen are outdated or confusing. Also, some upvoted answers are contradict.
I am trying to print error messages in red color. I am using ANSI code to achieve it.
debugPrint("\x1B[31m HelloDebug \x1B[0m");
The above one print in red color.
But when i do the same using log(), its not printing in red color. Its escaping the ANSI code.
One thing I found out was log() has error parameter.
If i pass something as error , it handles red color by default.
Here in the first one using log, red is not coming because ANSI code is ignored. But in debugPrint using it's working fine. In third one it takes error red by default. But extra line is added even for empty string message.
Code:
Output:
Is it possible use ANSI code for making color text using log?
Because I have other ANSI code to make different color text.
I dont want use debugPrint because if I print some thing too frequently , the system ignore the logs.
After digging some time I found out that log function is implemented in c++ in the dart runtime.As the log function is external function in dart sdk.
external void log(
String message, {
DateTime? time,
int? sequenceNumber,
int level = 0,
String name = '',
Zone? zone,
Object? error,
StackTrace? stackTrace,
});
https://github.com/dart-lang/sdk/blob/main/runtime/lib/developer.cc
Also I see some different answers while digging.
The below answer says debugPrint only available inside widget class. Is it still true?
https://stackoverflow.com/a/52241553/9248098
EDIT:
While using debugPrint and when I launch app from Android studio terminal, the ANSI color is working in android but when I run it same in iOS its escaping the characters in same Android Studio terminal.
If ANSI code support is based on terminal support, I couldn't figure out why its having issue in same terminal in iOS.
It is well explained in this page Debugging Flutter apps programmatically
debugPrint() is similar to print() but with a throttles applied to prevents being dropped by Android’s kernel.
If you have linter setup on VSCode or other IDE, you will see this warning whenever you use print in your code. For more information, you can refer to Avoid print calls in production code.
To avoid the lint error, you can use kDebugMode with print or debugPrint instead.
if (kDebugMode) {
print('test print');
}
// OR
debugPrint('test debugPrint');
As for log(), it actually allows you to include more information on the logging output. For example, you can add the runtimeType and the relevant object in the log that can help you in debugging the code.
log(
'test log',
name: runtimeType.toString(),
error: myObj,
);
** btw I tried log with ANSI code log("\x1B[31m HelloDebug \x1B[0m");, it is working as expected.

How to log with stacktrace and not show in release version?

I tried to handle exception like this.
try {
// some functions that likely to throw error
} catch (e) {
print(e);
}
but this print(e) didn't tell me where error come from, it only tell me what error is.
I want to know where error come from and have links that navigate to that line. like this
I also don't want to log on release version, so print is not my anwser.
Can you please give any example on how to do this?
There's a couple of options here depending on whether or not you'll want to be able to control logging output throughout your application or if this is a one-off situation.
Solution 1: make use of asserts
In Dart, assert(...) invocations are only included in debug builds (e.g. processes started via flutter run or dart --enable-asserts foo.dart). You can utilize this fact to conditionally execute code depending on whether or not your program is running in release by doing the following:
assert(() {
print('debug-only logging');
}());
Solution 2: use package:logging
A more general solution is to use package:logging to set up a global Logger instance and then only log through that interface. This package lets you write logs with different verbosities and the level property of Logger can be used to determine which log levels are actually printed. This can be combined with solution #1 to conditionally enable logging throughout the application only while debugging:
void main() {
// Disable logs by default.
myLogger.level = Level.OFF;
assert(() {
// Enable logs in debug mode.
myLogger.level = Level.ALL;
}());
// Your logic here
// ...
}

How to print info messages in dart:test?

When writing tests with dart:test how to print info messages such that they appear interleaved with tests output?
If I use print then it prints in the end, after all tests output.
Looking for analog of info() in ScalaTest.
After clarification with guys from the dart:test dev team it looks like regular print can be used. You just need to pass the --reporter=expanded argument when running
pub run test test/shimlaw_tests_test.dart --reporter=expanded
By default a compact single-line reporter is used which places output of print in the end of the test runner output. While the expanded reporter prints appropriately.

Is there support for predefined macros in DART

Does DART support predefined macros such as:
__LINE__
or
__FUNCTION__
The reason for asking is that the transformer DART2JS makes the console log not useful as all the logs shows: js_primitives.dart:30
[update BasE]
When using the transformer dart2js, print("hello world"); will result in:
JS('void', r'console.log(#)', "hello world);
to be invoked from function: printString(String string)
residing in the library dart2js._js_primitives
This results that the console.log message always contains the same line number over and over again wherever in the DART code a print(); is used. (As console.log will add automatically the filename and line-number to the console display of the wrapper function residing in dart2js._js_primitives)
As the current implementation of adding file-name and line-number to the console.log message is useless, it would have been nice if there would be another method that allows to display additional information.
As example, print("hello world" __FUNCTION__ __LINE__); would result in additional debug information that can be more useful.
You could use
void main() {
print(StackTrace.current);
}
to get better information about the source of the error
DartPad example
You can also run your code in a custom zone and define a custom print method for that zone. See also https://api.dartlang.org/stable/1.24.3/dart-async/Zone/print.html
Seems what you are looking for is source maps that dart2js creates, that contain the information needed to recreate line numbers in original dart files, from the javascript locations.

Write to the system's standard error in Progress

I am writing a small program in Progress that needs to write an error message to the system's standard error. What ways, simple if at all possible, can I use to print to standard error?
I am using OpenEdge 11.3.
When on Windows (10.2B+) you can use .NET:
System.Console:Error:WriteLine ("This is an error message") .
together with
prowin32 2> stderr.out
Progress doesn't provide a way to write to stderr - the easiest way I can think of is to output-through an external program that takes stdin and echoes it to stderr.
You could look into LOG-MANAGER:WRITE-MESSAGE. It won't log to standard output or standard error, but to a client-specific log. This log should be monitored in any case (specifically if the client is an application server).
From the documentation:
For an interactive or batch client, the WRITE-MESSAGE( ) method writes the log entries to the log file specified by the LOGFILE-NAME attribute or the Client Logging (-clientlog) startup parameter. For WebSpeed agents and AppServer servers, the WRITE-MESSAGE() method writes the log entries to the server log file. For DataServers, the WRITE-MESSAGE() method writes the log entries to the log file specified by the DataServer Logging (-dslog) startup parameter.
LOG-MANAGER:WRITE-MESSAGE("Got here, x=" + STRING(x), "DEBUG1").
Will write this in the log:
[04/12/05#13:19:19.742-0500] P-003616 T-001984 1 4GL DEBUG1 Got here, x=5
There are quite a lot of options regarding the LOG-MANAGER system, what messages to display, where the file is placed, etc.
There is no easy way, but in Unixen you can always do something like this using OUTPUT THROUGH (untested):
output through "cat >&2" no-echo unbuffered.
Alternatively -- and this is tested -- if you just want error messages from a batch-mode program to go to standard out then
output through "tee" ...
...definitely works.

Resources