github.com/UNO-SOFT/zlog/v2 
NewT(t).SLog()
returns an *slog.Logger that uses t.Log for printing.

But maybe I don't understand your real problem.

shan...@gmail.com a következőt írta (2023. június 14., szerda, 2:14:27 
UTC+2):

> In the past when I wanted to 'capture' the log output so that I could 
> check it in a unit test I would 'hijack' os.Stdout like so
> ```
> var osStdout = os.Stdout
> func MyCode (){
>   log.SetOutput(osStdout)
>   log.Print("My dog has fleas")
> }
> ```
> Which could then be tested thus
> ```
> func TestMyCode(t *testing.T){
>   testcases := map[string]struct{
>   output string
>   }{
>     "Dog needs flea shampoo": {
>       output: "My dog has fleas",
>     },
>   }
>   for name, tc := range testcases {
>       t.Run(name, func(t *testing.T) {
>          orig := osStdout
>          flags := log.Flags()
>          log.SetFlags(0)
>          reader, writer, err := os.Pipe()
>          if err != nil {
>             panic(err)
>          }
>          osStdout = writer
>          defer func() {
>            osStdout = orig
>            log.SetFlags(flags)
>           }()
>          MyCode()
>          writer.Close()
>
>          var buf strings.Builder
>          if _, ioerr := io.Copy(&buf, reader); ioerr != nil {
>            log.Fatalf("Copy error, cannot continue %v\n", ioerr)
>          }
>
>           assert.Equal(t, tc.output, buf.String(), "Expected: %s Got: %s", 
> tc.output, buf.String())
>          }
>        })
>     }
> }
> ```
>
> I'm now trying to do the same with the slog package (but not having any 
> joy) - is there an established pattern that people are using, eg. is the 
> handler being
>
> I've been trying 
> ```
> func TestMyCode(t *testing.T){
>   testcases := map[string]struct{
>   output string
>   }{
>     "Dog needs flea shampoo": {
>       output: "My dog has fleas",
>     },
>   }
>   for name, tc := range testcases {
>       t.Run(name, func(t *testing.T) {
>          orig := osStdout
>          reader, writer, err := os.Pipe()
>          if err != nil {
>             panic(err)
>          }
>          osStdout = writer
>          defer func() {
>            osStdout = orig
>           }()
>          slog.SetDefault(slog.New(slog.NewTextHandler(osStdout)
>          MyCode()
>          writer.Close()
>
>          var buf strings.Builder
>          if _, ioerr := io.Copy(&buf, reader); ioerr != nil {
>            log.Fatalf("Copy error, cannot continue %v\n", ioerr)
>          }
>
>           assert.Equal(t, tc.output, buf.String(), "Expected: %s Got: %s", 
> tc.output, buf.String())
>          }
>        })
>     }
> }
> ```
>
> Which "works" but I am not happy with it, because it's affecting all of 
> the slog instances.
>
> I saw previously that someone was creating their own implementation of a 
> slog.Handler? to make testing easier, and would love to see it, and will 
> that allow me to run multiple slog based tests concurrently?
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/9f9449cd-b04b-4b9f-b7ce-639e80e7a708n%40googlegroups.com.

Reply via email to