Thread overview
looking for recommendation: which thread safe logger library?
Jul 12, 2023
mw
Jul 12, 2023
mw
Jul 12, 2023
mw
Jul 12, 2023
Danilo
Jul 12, 2023
mw
Jul 12, 2023
Danilo
Jul 12, 2023
mw
Jul 12, 2023
Danilo
July 12, 2023

Hi,

Just wondering which logger library is thread safe?

https://code.dlang.org/

Need to find a mature one to be used in a multi-threaded env.

Thanks.

July 12, 2023

On Wednesday, 12 July 2023 at 01:24:41 UTC, mw wrote:

>

Hi,

Just wondering which logger library is thread safe?

https://code.dlang.org/

Need to find a mature one to be used in a multi-threaded env.

Oh, forget to mention: need output to logger file.

July 12, 2023

On Wednesday, 12 July 2023 at 01:26:25 UTC, mw wrote:

>

On Wednesday, 12 July 2023 at 01:24:41 UTC, mw wrote:

>

Hi,

Just wondering which logger library is thread safe?

https://code.dlang.org/

Need to find a mature one to be used in a multi-threaded env.

Oh, forget to mention: need output to logger file.

tried:

https://run.dlang.io/is/ufy4yF

import std.experimental.logger;

void main() {
    std.experimental.logger.sharedLog.trace("msg");
}

but:

onlineapp.d(4): Error: none of the overloads of template std.logger.core.Logger.memLogFunctions!LogLevel.trace.logImpl are callable using argument types !()(string) shared
/dlang/dmd/linux/bin64/../../src/phobos/std/logger/core.d(734): Candidates are: logImpl(int line = __LINE__, string file = __FILE__, string funcName = __FUNCTION__, string prettyFuncName = __PRETTY_FUNCTION__, string moduleName = __MODULE__, A...)(lazy A args)
/dlang/dmd/linux/bin64/../../src/phobos/std/logger/core.d(780): logImpl(int line = __LINE__, string file = __FILE__, string funcName = __FUNCTION__, string prettyFuncName = __PRETTY_FUNCTION__, string moduleName = __MODULE__, A...)(lazy bool condition, lazy A args)

so, how to fix this compiler error?

July 12, 2023

On Wednesday, 12 July 2023 at 01:55:00 UTC, mw wrote:

>
import std.experimental.logger;

void main() {
    std.experimental.logger.sharedLog.trace("msg");
}

See examples at https://dlang.org/phobos/std_logger.html
and https://dlang.org/phobos/std_logger_filelogger.html

import std.stdio;
import std.logger;

void main() {
    //auto file = File("logFile.log", "w");
    auto file = stderr; // stdout

    auto logger = new FileLogger(file);
    //auto logger = new FileLogger(file, LogLevel.fatal);

    logger.log("log message");
    logger.info("info message");
    logger.warning(5 < 6, "Logging with LogLevel.warning if 5 is less than 6");
    logger.warningf(5 < 6, "Logging with LogLevel.warning if %s is %s than 6", 5, "less");
    logger.critical("Logging with critical LogLevel");
    logger.log(LogLevel.trace, 5 < 6, "Logging"," with its default LogLevel if 5 is less than 6");
    //logger.fatal("fatal message");
}
July 12, 2023

On Wednesday, 12 July 2023 at 04:48:23 UTC, Danilo wrote:

>

On Wednesday, 12 July 2023 at 01:55:00 UTC, mw wrote:

>
import std.experimental.logger;

void main() {
    std.experimental.logger.sharedLog.trace("msg");
}

See examples at https://dlang.org/phobos/std_logger.html
and https://dlang.org/phobos/std_logger_filelogger.html

But what's wrong with my code? the strange compiler error?

July 12, 2023

On Wednesday, 12 July 2023 at 05:27:27 UTC, mw wrote:

>

But what's wrong with my code? the strange compiler error?

Might be a bug/issue in the logger module.

sharedLog uses the shared attribute,
but the base class for everything ("abstract class Logger")
does not use the shared attribute anywhere.

It works when you cast away the shared attribute from sharedLog,
so I think that's the problem.

import std.stdio;
import std.logger;

void main() {

    stdThreadLocalLog = new FileLogger(stderr);
    stdThreadLocalLog.trace("local msg");

    //-----------------------------------------------

    sharedLog = cast(shared)new FileLogger(stderr);

    //sharedLog.trace("shared msg");                                          // ERROR
    (cast()sharedLog).trace("shared msg");                                    // WORKS, cast away 'shared'

    //sharedLog.memLogFunctions!(LogLevel.trace).logImpl("shared msg");       // ERROR
    (cast()sharedLog).memLogFunctions!(LogLevel.trace).logImpl("shared msg"); // WORKS, cast away 'shared'
}
July 12, 2023

WebFreak said you can just use trace(), info() etc. inside threads. It is thread-safe by default.

module app;

import std.stdio;
import std.logger;

void main() {

    //auto file = File("logFile.log", "w");
    auto file = stderr; // stdout

    sharedLog = cast(shared)new FileLogger(file);

    import core.thread : Thread, msecs;

    Thread[100] threads;

    for(int i=0; i<threads.length; i++) {
        threads[i] = new Thread(
            function void() {
                info("thread info");
                trace("thread trace");
            }
        );
        threads[i].start();
    }

    for(int i=0; i<threads.length; i++) {
        threads[i].join();
    }
}
July 12, 2023

On Wednesday, 12 July 2023 at 09:47:26 UTC, Danilo wrote:

>

On Wednesday, 12 July 2023 at 05:27:27 UTC, mw wrote:

>

But what's wrong with my code? the strange compiler error?

Might be a bug/issue in the logger module.

sharedLog uses the shared attribute,
but the base class for everything ("abstract class Logger")
does not use the shared attribute anywhere.

It works when you cast away the shared attribute from sharedLog,

cast away? the variable itself is called sharedLog, the cast completely defeat the purpose.

Sigh, D is so broken on such basic stuff.