Search code examples
loggingkdb

For redirecting standard output in kdb, how to direct everything but results?


So for a query function, it product tables. How do I redirect standard output without returning the result? I want to redirect standard output without redirecting the table results. I want to catch every messages kdb produces.

So I tried this

q abc.q -p 4000 < /dev/null > kdb.log 2>&1 &.

but it also redirect the results from queries, right?

If only direct errors, that doesn't catch everything, right? I want to catch everything but the result.


Solution

  • Check out the below.. (I wrote this for a home project)

    .logger.on:{[pth]
      if[not ":/"~2#string[pth];-1"\n\tPlease supply absolute path to file; e.g.) .logger.on[`:/path/to/logfile]";];
     .logger.err:hopen `$string[pth],".err";
     .logger.std:hopen `$string[pth],".out";
     writeOut:{ h:(.logger.std;.logger.err)x~`err; h(y,"\n");};
     zpi:{y:-1_y; r:@[value;y;{"Error:'",x}]; $["Error:"~6#r;[e:6_r;x[`err;e];-1 e];[x[`std;y];show r]];}[writeOut;];
     `.z.pi set zpi;
     }
    

    After loading into q, type some commands...

    q).logger.on[`:/home/chromozorz/q/projects/myLog]
    q)2+2
    4
    q)1+1
    2
    q)t:([] a:til 5;b:5?`2)
    q)2+string
    'type
    q)2+`a
    'type
    q)\\
    

    Now check out the created log files;

    chromozorz@chromozorz:~/q/projects$ cat myLog.err
    'type
    'type
    chromozorz@chromozorz:~/q/projects$ cat myLog.out
    2+2
    1+1
    t:([] a:til 5;b:5?`2)
    

    If you don't want to show the result in the console then just edit the zpi definition...