uwsgi + flask logging.config无法正常工作,也会破坏应用程序

时间:2018-02-16 23:49:10

标签: python-2.7 logging flask uwsgi

一直在寻找过去5个小时的答案而一无所获。

我有一个烧瓶(python 2.7)应用程序正常使用uwsgi,但我没有日志。

/etc/uwsgi/uwsgi.ini config:

## initialize libraries
# devtools::install_github("mlr-org/mlr) # using developper version of mlr
if(!require("pacman")) install.packages("pacman")
pacman::p_load("mlr", "ParamHelpers", "e1071", "parallelMap")


## create tuning setting
svm.ps <- ParamHelpers::makeParamSet(
  ParamHelpers::makeNumericParam("cost", lower = -12, 
                                 upper = 15, trafo = function(x) 2^x),
  ParamHelpers::makeNumericParam("gamma", lower = -15, 
                                 upper = 6, trafo = function(x) 2^x)
)

## create random search grid, small iteration number for example
ctrl.tune <- mlr::makeTuneControlRandom(maxit = 8) 

# inner resampling loop, "
inner <- mlr::makeResampleDesc("SpCV", iters = 3, predict = "both")

# outer loop, "
outer <- mlr::makeResampleDesc("SpRepCV", folds = 5, reps = 2, predict = "both")


## create learner - Support Vector Machine of the e1071-package
lrn.svm <- mlr::makeLearner("classif.svm", predict.type = "prob")

# ... tuning in inner resampling
lrn.svm.tune <- mlr::makeTuneWrapper(learner = lrn.svm, resampling = inner, 
                                     measures = list(auc),
                                     par.set = svm.ps, control = ctrl.tune, 
                                     show.info = FALSE) 


## create function that calculate variable importance based on permutation 
extractVarImpFunction <- function(x)
{
  list(mlr::generateFeatureImportanceData(task = mlr::makeClassifTask(
                          id = x$task.desc$id, 
                          data = mlr::getTaskData(mlr::spatial.task, subset = x$subset), 
                          target = x$task.desc$target,
                          positive = x$task.desc$positive, 
                          coordinates = mlr::spatial.task$coordinates[x$subset,]),
                        method = "permutation.importance", 
                        learner = mlr::makeLearner(cl = "classif.svm", 
                                                     predict.type = "prob", 
                          cost = x$learner.model$opt.result$x$cost,
                          gamma = x$learner.model$opt.result$x$gamma),
                        measure = list(mlr::auc), nmc = 10
                          )$res
      )
}



## start resampling for getting variable importance of tuned models (outer)

# parallelize tuning
parallelMap::parallelStart(mode = "multicore", level = "mlr.tuneParams", cpus = 8)

res.VarImpTuned <- mlr::resample(learner = lrn.svm.tune, task = mlr::spatial.task, 
                                 extract = extractVarImpFunction,
                                 resampling = outer, measures = list(auc), 
                                 models = TRUE, show.info = TRUE)

parallelMap::parallelStop() # stop parallelization

## get mean auroc decrease
var.imp <- do.call(rbind, lapply(res.VarImpTuned$extract, FUN = function(x){x[[1]]}))
var.imp <- data.frame(AUC_DECR = colMeans(var.imp), Variable = names(colMeans(var.imp))) 

/app/uwsgi.ini

[uwsgi]
socket = /tmp/uwsgi.sock
chown-socket = nginx:nginx
chmod-socket = 664
cheaper = 2
processes = 16

/app/main.py(没有uwsgi就可以正常工作)

[uwsgi]
module = main
callable = app

logging.config.json:

app = Flask(__name__)
...
...
...
if __name__ == "__main__":
    with open("{0}/logging.config.json".format(CONFIG_PATH), "r") as fd:
        logging.config.dictConfig(json.load(fd))
    app.run()

我还尝试将记录器移到main(一个帖子建议)之外,如下所示:

...
"formatters": {
  "simple": {
      "()": "pythonjsonlogger.jsonlogger.JsonFormatter",
      "format": "%(asctime)s %(levelname)s %(module)s %(message)s"
    }
...
"handlers": {
"console":{
      "level": "DEBUG",
      "class": "logging.StreamHandler",
      "formatter": "simple",
      "stream" : "ext://sys.stdout"
  },
  "loggers": { },
  "root": {
      "handlers": ["console"],
      "level": "DEBUG"
  }
}

它只是打破了uwsgi:

app = Flask(__name__)

with open("{0}/logging.config.json".format(CONFIG_PATH), "r") as fd:
    logging.config.dictConfig(json.load(fd))
...
...
...
if __name__ == "__main__":
    app.run()

与app声明之前的日志声明相同(但没有错误 - 应用程序不起作用)

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我发现了问题:

"formatters": {
"simple": {
  "()": "pythonjsonlogger.jsonlogger.JsonFormatter",
  "format": "%(asctime)s %(levelname)s %(module)s %(message)s"
}

pythonjsonlogger是我几十年前手动安装的库而忘了。声明logging.config.dictConfig()。

时未正确加载

此外,对于任何人来说,uWSGI只调用app.run(),如果你想查看任何日志,你应该在声明app之前声明日志记录。例如:

with open("{0}/logging.config.json".format(CONFIG_PATH), "r") as fd:
    logging.config.dictConfig(json.load(fd))

app = Flask(__name__)
...
...
...
if __name__ == "__main__":
    app.run()