2020-10-21 05:17:48 -04:00
|
|
|
// Copyright 2020 The Hugo Authors. All rights reserved.
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package loggers
|
|
|
|
|
|
|
|
import (
|
2018-10-03 08:58:09 -04:00
|
|
|
"bytes"
|
2019-10-23 16:04:52 -04:00
|
|
|
"fmt"
|
2018-10-03 08:58:09 -04:00
|
|
|
"io"
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
"log"
|
|
|
|
"os"
|
2018-10-24 05:14:51 -04:00
|
|
|
"regexp"
|
2019-10-23 16:04:52 -04:00
|
|
|
"runtime"
|
2023-03-14 07:18:42 -04:00
|
|
|
"sync/atomic"
|
2019-11-21 07:07:52 -05:00
|
|
|
"time"
|
2018-10-24 05:14:51 -04:00
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/common/terminal"
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
|
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
|
|
|
)
|
|
|
|
|
2022-01-06 04:22:19 -05:00
|
|
|
var (
|
|
|
|
// Counts ERROR logs to the global jww logger.
|
|
|
|
GlobalErrorCounter *jww.Counter
|
2023-03-14 07:18:42 -04:00
|
|
|
PanicOnWarning atomic.Bool
|
2022-01-06 04:22:19 -05:00
|
|
|
)
|
2018-10-03 08:58:09 -04:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
GlobalErrorCounter = &jww.Counter{}
|
|
|
|
jww.SetLogListeners(jww.LogCounter(GlobalErrorCounter, jww.LevelError))
|
|
|
|
}
|
|
|
|
|
2020-08-20 12:43:09 -04:00
|
|
|
func LoggerToWriterWithPrefix(logger *log.Logger, prefix string) io.Writer {
|
|
|
|
return prefixWriter{
|
|
|
|
logger: logger,
|
|
|
|
prefix: prefix,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type prefixWriter struct {
|
|
|
|
logger *log.Logger
|
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w prefixWriter) Write(p []byte) (n int, err error) {
|
|
|
|
w.logger.Printf("%s: %s", w.prefix, p)
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2020-10-21 05:17:48 -04:00
|
|
|
type Logger interface {
|
2022-03-17 17:03:27 -04:00
|
|
|
Printf(format string, v ...any)
|
|
|
|
Println(v ...any)
|
2020-10-21 05:17:48 -04:00
|
|
|
PrintTimerIfDelayed(start time.Time, name string)
|
|
|
|
Debug() *log.Logger
|
2022-03-17 17:03:27 -04:00
|
|
|
Debugf(format string, v ...any)
|
|
|
|
Debugln(v ...any)
|
2020-10-21 05:17:48 -04:00
|
|
|
Info() *log.Logger
|
2022-03-17 17:03:27 -04:00
|
|
|
Infof(format string, v ...any)
|
|
|
|
Infoln(v ...any)
|
2020-10-21 05:17:48 -04:00
|
|
|
Warn() *log.Logger
|
2022-03-17 17:03:27 -04:00
|
|
|
Warnf(format string, v ...any)
|
|
|
|
Warnln(v ...any)
|
2020-10-21 05:17:48 -04:00
|
|
|
Error() *log.Logger
|
2022-03-17 17:03:27 -04:00
|
|
|
Errorf(format string, v ...any)
|
|
|
|
Errorln(v ...any)
|
2020-10-21 05:17:48 -04:00
|
|
|
Errors() string
|
|
|
|
|
|
|
|
Out() io.Writer
|
|
|
|
|
|
|
|
Reset()
|
|
|
|
|
|
|
|
// Used in tests.
|
|
|
|
LogCounters() *LogCounters
|
|
|
|
}
|
|
|
|
|
|
|
|
type LogCounters struct {
|
|
|
|
ErrorCounter *jww.Counter
|
|
|
|
WarnCounter *jww.Counter
|
|
|
|
}
|
|
|
|
|
|
|
|
type logger struct {
|
2018-10-03 08:58:09 -04:00
|
|
|
*jww.Notepad
|
2019-11-21 07:07:52 -05:00
|
|
|
|
|
|
|
// The writer that represents stdout.
|
2023-02-18 17:43:26 -05:00
|
|
|
// Will be io.Discard when in quiet mode.
|
2020-10-21 05:17:48 -04:00
|
|
|
out io.Writer
|
2019-11-21 07:07:52 -05:00
|
|
|
|
2020-10-21 05:17:48 -04:00
|
|
|
logCounters *LogCounters
|
2018-10-03 08:58:09 -04:00
|
|
|
|
|
|
|
// This is only set in server mode.
|
2018-10-24 11:22:07 -04:00
|
|
|
errors *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (l *logger) Printf(format string, v ...any) {
|
2020-10-21 05:17:48 -04:00
|
|
|
l.FEEDBACK.Printf(format, v...)
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (l *logger) Println(v ...any) {
|
2020-10-21 05:17:48 -04:00
|
|
|
l.FEEDBACK.Println(v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Debug() *log.Logger {
|
|
|
|
return l.DEBUG
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (l *logger) Debugf(format string, v ...any) {
|
2021-06-07 10:36:48 -04:00
|
|
|
l.DEBUG.Printf(format, v...)
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (l *logger) Debugln(v ...any) {
|
2021-06-07 10:36:48 -04:00
|
|
|
l.DEBUG.Println(v...)
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (l *logger) Infof(format string, v ...any) {
|
2020-10-21 05:17:48 -04:00
|
|
|
l.INFO.Printf(format, v...)
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (l *logger) Infoln(v ...any) {
|
2020-10-21 05:17:48 -04:00
|
|
|
l.INFO.Println(v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Info() *log.Logger {
|
|
|
|
return l.INFO
|
|
|
|
}
|
|
|
|
|
2022-01-27 11:13:32 -05:00
|
|
|
const panicOnWarningMessage = "Warning trapped. Remove the --panicOnWarning flag to continue."
|
2022-01-06 04:22:19 -05:00
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (l *logger) Warnf(format string, v ...any) {
|
2020-10-21 05:17:48 -04:00
|
|
|
l.WARN.Printf(format, v...)
|
2023-03-14 07:18:42 -04:00
|
|
|
if PanicOnWarning.Load() {
|
2022-01-06 04:22:19 -05:00
|
|
|
panic(panicOnWarningMessage)
|
|
|
|
}
|
2020-10-21 05:17:48 -04:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (l *logger) Warnln(v ...any) {
|
2020-10-21 05:17:48 -04:00
|
|
|
l.WARN.Println(v...)
|
2023-03-14 07:18:42 -04:00
|
|
|
if PanicOnWarning.Load() {
|
2022-01-06 04:22:19 -05:00
|
|
|
panic(panicOnWarningMessage)
|
|
|
|
}
|
2020-10-21 05:17:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Warn() *log.Logger {
|
|
|
|
return l.WARN
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (l *logger) Errorf(format string, v ...any) {
|
2020-10-21 05:17:48 -04:00
|
|
|
l.ERROR.Printf(format, v...)
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (l *logger) Errorln(v ...any) {
|
2020-10-21 05:17:48 -04:00
|
|
|
l.ERROR.Println(v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Error() *log.Logger {
|
|
|
|
return l.ERROR
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) LogCounters() *LogCounters {
|
|
|
|
return l.logCounters
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Out() io.Writer {
|
|
|
|
return l.out
|
|
|
|
}
|
|
|
|
|
2019-11-21 07:07:52 -05:00
|
|
|
// PrintTimerIfDelayed prints a time statement to the FEEDBACK logger
|
|
|
|
// if considerable time is spent.
|
2020-10-21 05:17:48 -04:00
|
|
|
func (l *logger) PrintTimerIfDelayed(start time.Time, name string) {
|
2022-05-09 04:05:19 -04:00
|
|
|
elapsed := time.Since(start)
|
2019-11-21 07:07:52 -05:00
|
|
|
milli := int(1000 * elapsed.Seconds())
|
|
|
|
if milli < 500 {
|
|
|
|
return
|
|
|
|
}
|
2020-10-21 05:17:48 -04:00
|
|
|
l.Printf("%s in %v ms", name, milli)
|
2019-11-21 07:07:52 -05:00
|
|
|
}
|
|
|
|
|
2020-10-21 05:17:48 -04:00
|
|
|
func (l *logger) PrintTimer(start time.Time, name string) {
|
2022-05-09 04:05:19 -04:00
|
|
|
elapsed := time.Since(start)
|
2019-11-21 07:07:52 -05:00
|
|
|
milli := int(1000 * elapsed.Seconds())
|
2020-10-21 05:17:48 -04:00
|
|
|
l.Printf("%s in %v ms", name, milli)
|
2019-11-21 07:07:52 -05:00
|
|
|
}
|
|
|
|
|
2020-10-21 05:17:48 -04:00
|
|
|
func (l *logger) Errors() string {
|
2018-10-24 11:22:07 -04:00
|
|
|
if l.errors == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ansiColorRe.ReplaceAllString(l.errors.String(), "")
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset resets the logger's internal state.
|
2020-10-21 05:17:48 -04:00
|
|
|
func (l *logger) Reset() {
|
|
|
|
l.logCounters.ErrorCounter.Reset()
|
2018-10-24 11:22:07 -04:00
|
|
|
if l.errors != nil {
|
|
|
|
l.errors.Reset()
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-10 14:18:41 -05:00
|
|
|
// NewLogger creates a new Logger for the given thresholds
|
2020-10-21 05:17:48 -04:00
|
|
|
func NewLogger(stdoutThreshold, logThreshold jww.Threshold, outHandle, logHandle io.Writer, saveErrors bool) Logger {
|
2018-10-03 08:58:09 -04:00
|
|
|
return newLogger(stdoutThreshold, logThreshold, outHandle, logHandle, saveErrors)
|
|
|
|
}
|
|
|
|
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
// NewDebugLogger is a convenience function to create a debug logger.
|
2020-10-21 05:17:48 -04:00
|
|
|
func NewDebugLogger() Logger {
|
2020-08-20 12:43:09 -04:00
|
|
|
return NewBasicLogger(jww.LevelDebug)
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewWarningLogger is a convenience function to create a warning logger.
|
2020-10-21 05:17:48 -04:00
|
|
|
func NewWarningLogger() Logger {
|
2020-08-20 12:43:09 -04:00
|
|
|
return NewBasicLogger(jww.LevelWarn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewInfoLogger is a convenience function to create a info logger.
|
2020-10-21 05:17:48 -04:00
|
|
|
func NewInfoLogger() Logger {
|
2020-08-20 12:43:09 -04:00
|
|
|
return NewBasicLogger(jww.LevelInfo)
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewErrorLogger is a convenience function to create an error logger.
|
2020-10-21 05:17:48 -04:00
|
|
|
func NewErrorLogger() Logger {
|
2020-08-20 12:43:09 -04:00
|
|
|
return NewBasicLogger(jww.LevelError)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBasicLogger creates a new basic logger writing to Stdout.
|
2020-10-21 05:17:48 -04:00
|
|
|
func NewBasicLogger(t jww.Threshold) Logger {
|
2023-02-18 17:43:26 -05:00
|
|
|
return newLogger(t, jww.LevelError, os.Stdout, io.Discard, false)
|
2020-08-20 12:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBasicLoggerForWriter creates a new basic logger writing to w.
|
2020-10-21 05:17:48 -04:00
|
|
|
func NewBasicLoggerForWriter(t jww.Threshold, w io.Writer) Logger {
|
2023-02-18 17:43:26 -05:00
|
|
|
return newLogger(t, jww.LevelError, w, io.Discard, false)
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2022-05-12 05:43:20 -04:00
|
|
|
// RemoveANSIColours removes all ANSI colours from the given string.
|
|
|
|
func RemoveANSIColours(s string) string {
|
|
|
|
return ansiColorRe.ReplaceAllString(s, "")
|
|
|
|
}
|
|
|
|
|
2018-10-24 12:32:30 -04:00
|
|
|
var (
|
|
|
|
ansiColorRe = regexp.MustCompile("(?s)\\033\\[\\d*(;\\d*)*m")
|
2018-11-01 04:33:32 -04:00
|
|
|
errorRe = regexp.MustCompile("^(ERROR|FATAL|WARN)")
|
2018-10-24 12:32:30 -04:00
|
|
|
)
|
2018-10-24 05:14:51 -04:00
|
|
|
|
|
|
|
type ansiCleaner struct {
|
|
|
|
w io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a ansiCleaner) Write(p []byte) (n int, err error) {
|
|
|
|
return a.w.Write(ansiColorRe.ReplaceAll(p, []byte("")))
|
|
|
|
}
|
|
|
|
|
2018-10-24 12:32:30 -04:00
|
|
|
type labelColorizer struct {
|
|
|
|
w io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a labelColorizer) Write(p []byte) (n int, err error) {
|
|
|
|
replaced := errorRe.ReplaceAllStringFunc(string(p), func(m string) string {
|
|
|
|
switch m {
|
|
|
|
case "ERROR", "FATAL":
|
|
|
|
return terminal.Error(m)
|
|
|
|
case "WARN":
|
|
|
|
return terminal.Warning(m)
|
|
|
|
default:
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// io.MultiWriter will abort if we return a bigger write count than input
|
|
|
|
// bytes, so we lie a little.
|
|
|
|
_, err = a.w.Write([]byte(replaced))
|
|
|
|
return len(p), err
|
|
|
|
}
|
|
|
|
|
2018-11-13 12:28:40 -05:00
|
|
|
// InitGlobalLogger initializes the global logger, used in some rare cases.
|
2018-10-28 11:06:50 -04:00
|
|
|
func InitGlobalLogger(stdoutThreshold, logThreshold jww.Threshold, outHandle, logHandle io.Writer) {
|
|
|
|
outHandle, logHandle = getLogWriters(outHandle, logHandle)
|
|
|
|
|
|
|
|
jww.SetStdoutOutput(outHandle)
|
|
|
|
jww.SetLogOutput(logHandle)
|
|
|
|
jww.SetLogThreshold(logThreshold)
|
|
|
|
jww.SetStdoutThreshold(stdoutThreshold)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLogWriters(outHandle, logHandle io.Writer) (io.Writer, io.Writer) {
|
2022-06-13 09:28:08 -04:00
|
|
|
isTerm := terminal.PrintANSIColors(os.Stdout)
|
2023-02-18 17:43:26 -05:00
|
|
|
if logHandle != io.Discard && isTerm {
|
2018-10-24 05:14:51 -04:00
|
|
|
// Remove any Ansi coloring from log output
|
|
|
|
logHandle = ansiCleaner{w: logHandle}
|
|
|
|
}
|
2018-10-24 12:32:30 -04:00
|
|
|
|
|
|
|
if isTerm {
|
|
|
|
outHandle = labelColorizer{w: outHandle}
|
|
|
|
}
|
|
|
|
|
2018-10-28 11:06:50 -04:00
|
|
|
return outHandle, logHandle
|
|
|
|
}
|
|
|
|
|
2019-10-23 16:04:52 -04:00
|
|
|
type fatalLogWriter int
|
|
|
|
|
|
|
|
func (s fatalLogWriter) Write(p []byte) (n int, err error) {
|
|
|
|
trace := make([]byte, 1500)
|
|
|
|
runtime.Stack(trace, true)
|
|
|
|
fmt.Printf("\n===========\n\n%s\n", trace)
|
|
|
|
os.Exit(-1)
|
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var fatalLogListener = func(t jww.Threshold) io.Writer {
|
|
|
|
if t != jww.LevelError {
|
|
|
|
// Only interested in ERROR
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return new(fatalLogWriter)
|
|
|
|
}
|
|
|
|
|
2020-10-21 05:17:48 -04:00
|
|
|
func newLogger(stdoutThreshold, logThreshold jww.Threshold, outHandle, logHandle io.Writer, saveErrors bool) *logger {
|
2018-10-28 11:06:50 -04:00
|
|
|
errorCounter := &jww.Counter{}
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
warnCounter := &jww.Counter{}
|
2018-10-28 11:06:50 -04:00
|
|
|
outHandle, logHandle = getLogWriters(outHandle, logHandle)
|
|
|
|
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
listeners := []jww.LogListener{jww.LogCounter(errorCounter, jww.LevelError), jww.LogCounter(warnCounter, jww.LevelWarn)}
|
2018-10-03 08:58:09 -04:00
|
|
|
var errorBuff *bytes.Buffer
|
|
|
|
if saveErrors {
|
|
|
|
errorBuff = new(bytes.Buffer)
|
|
|
|
errorCapture := func(t jww.Threshold) io.Writer {
|
|
|
|
if t != jww.LevelError {
|
|
|
|
// Only interested in ERROR
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errorBuff
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners = append(listeners, errorCapture)
|
|
|
|
}
|
|
|
|
|
2020-10-21 05:17:48 -04:00
|
|
|
return &logger{
|
|
|
|
Notepad: jww.NewNotepad(stdoutThreshold, logThreshold, outHandle, logHandle, "", log.Ldate|log.Ltime, listeners...),
|
|
|
|
out: outHandle,
|
|
|
|
logCounters: &LogCounters{
|
|
|
|
ErrorCounter: errorCounter,
|
|
|
|
WarnCounter: warnCounter,
|
|
|
|
},
|
|
|
|
errors: errorBuff,
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
}
|