Exemple #1
0
func (data *Data) Decompress() error {
	if !data.IsCompressed() {
		return nil
	}
	input := bytes.NewBuffer(data.Content)
	var reader io.Reader
	if data.ContentEncoding == ceDeflate {
		reader = flate.NewReader(input)
	} else if data.ContentEncoding == ceGzip {
		var err error
		if reader, err = gzip.NewReader(input); err != nil {
			return err
		}
	} else {
		return log.Error("unknown content-encoding",
			log.WithValue("content-encoding", data.ContentEncoding))
	}
	writer := bytes.Buffer{}
	_, err := io.Copy(&writer, reader)
	if err != nil {
		return err
	}
	data.Content = writer.Bytes()
	data.ContentEncoding = ""
	data.UncompressedLength = len(data.Content)
	return nil
}
func ExampleWithError() error {
	if err := doSomething(); err != nil {
		return log.Error("doSomething failed",
			log.WithError(err))
	}

	// ... more processing and then ...

	return nil
}
func ExampleWithValue(n1, n2 int) error {
	if err := doSomethingWith(n1, n2); err != nil {
		return log.Error("doSomethingWith failed",
			log.WithValue("n1", n1),
			log.WithValue("n2", n2))
	}

	// ... more processing and then ...

	return nil
}
func ExampleOption(ctx context.Context, n1, n2 int) error {
	if err := doSomethingWith(n1, n2); err != nil {
		return log.Error("cannot doSomething",
			log.WithValue("n1", n1),
			log.WithValue("n2", n2),
			log.WithError(err),
			log.WithContext(ctx))
	}

	// .. more processing and then ...

	return nil
}
func ExampleNewContext(ctx context.Context, n1, n2 int) error {
	ctx = log.NewContext(ctx, "n1", n1)
	ctx = log.NewContext(ctx, "n2", n2)

	if err := doSomethingWith(n1, n2); err != nil {
		return log.Error("doSomethingWith failed",
			log.WithError(err),
			log.WithContext(ctx))
	}

	log.Debug("did something with",
		log.WithContext(ctx))

	// ... more processing and then ...

	return nil
}