Thursday, February 25, 2010

Grails Environment Specific Logging to a File

I need to log my Grails application across different environments. For example, a Grails embeded Jetty server in DEV environment and a Tomcat server in TEST environment. Different servers have different log directory locations. How to dynamically pick up the right location? The trick is to define the file location (a relative path in my case) as a global variable inside Config.groovy, customized it in the environment blocks, and use the variable location inside log4j closure.

The following is the Config.groovy:

def logDirectory = '.'
environments {
development {
}
test {
logDirectory = "../logs"
}
}

// log4j configuration
log4j = {
appenders {
console name:'stdout'
rollingFile name:'file', file: logDirectory + '/app.log', threshold: org.apache.log4j.Level.INFO, maxFileSize:"1MB", maxBackupIndex: 10, 'append':true
}

error 'org.codehaus.groovy.grails.web.servlet', // controllers
'org.codehaus.groovy.grails.web.pages', // GSP
'org.codehaus.groovy.grails.web.sitemesh', // layouts
'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
'org.codehaus.groovy.grails.web.mapping', // URL mapping
'org.codehaus.groovy.grails.commons', // core / classloading
'org.codehaus.groovy.grails.plugins', // plugins
'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration
'org.springframework',
'org.hibernate',
'org.codehaus.groovy.grails.scaffolding.view.ScaffoldingViewResolver'

warn 'org.mortbay.log'

root {
info 'stdout', 'file'
additivity = true
}
}


It has been tested to work on Grails version is 1.1.1 with Tomcat 6.0.

39 comments:

  1. need an update for Grails 2.0.1... the above does not work :(

    ReplyDelete
  2. Works for me on 2.0.1 (well I did not try to set the path within the environments section, but the overall concept seems to be fine)

    ReplyDelete
  3. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
    Best Devops training in sholinganallur
    Devops training in velachery
    Devops training in annanagar
    Devops training in tambaram

    ReplyDelete
  4. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    Selenium training in Chennai

    Selenium training in Bangalore

    Selenium training in Pune

    Selenium Online training

    ReplyDelete
  5. A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  6. I think you have a long story to share and i am glad after long time finally you cam and shared your experience.
    python Training in Pune
    python Training in Chennai
    python Training in Bangalore

    ReplyDelete
  7. It is better to engaged ourselves in activities we like. I liked the post. Thanks for sharing.
    python Training in Pune
    python Training in Chennai
    python Training in Bangalore

    ReplyDelete
  8. Có lẽ cần phải trải qua tuổi thanh xuân( Phương pháp học toán tư duy ) mới có thể hiểu được tuổi xuân là khoảng thời gian ta( dạy trẻ học toán tư duy ) sống ích kỷ biết chừng nào. Có lúc nghĩ, sở dĩ tình yêu cần phải đi một vòng tròn lớn như vậy, phải trả một cái giá quá đắt như thế,( Làm cho trẻ thích học toán ) là bởi vì nó đến không đúng thời điểm. Khi có được( Toán mầm non ) tình yêu, chúng ta thiếu đi trí tuệ. Đợi đến khi( Bé học đếm số ) có đủ trí tuệ, chúng ta đã không còn sức lực để yêu một tình yêu thuần khiết nữa.

    ReplyDelete
  9. Дээд чанар бол зүгээр л( đá ruby thiên nhiên ) санаатай биш юм. Энэ нь өндөр( đá ruby nam phi ) түвшний төвлөрөл, тусгай хүчин( Đá Sapphire ) чармайлт, ухаалаг ( đá sapphire hợp mệnh gì )чиг баримжаа, чадварлаг туршлага, ( đá ruby đỏ )саад тотгорыг даван туулах( bán đá sapphire thô ) боломжийг хардаг.

    ReplyDelete
  10. Congratulations This is the great things. Thanks to giving the time to share such a nice information.best Mulesoft training in bangalore

    ReplyDelete
  11. Very useful and information content has been shared out here, Thanks for sharing it.Mulesoft training in bangalore

    ReplyDelete
  12. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.web designing training in bangalore



    ReplyDelete
  13. Really i appreciate the effort you made to share the knowledge. The topic here i found was really effective...

    Softgen Infotech is the Best Oracle Training institute located in BTM Layout, Bangalore providing quality training with Realtime Trainers and 100% Job Assistance.

    ReplyDelete
  14. I really like it whenever people get together and share thoughts. Great website, stick with it!

    ReplyDelete
  15. I have read your blog and I got very useful and knowledgeable information from your blog. It’s really a very nice article.
    devops training in chennai | devops training in anna nagar | devops training in omr | devops training in porur | devops training in tambaram | devops training in velachery

    ReplyDelete
  16. Wow, amazing weblog format! How lengthy have you been running a blog for? you make running a blog look easy. The total glance of your website is wonderful, let alone the content!
    oracle training in chennai

    oracle training in velachery

    oracle dba training in chennai

    oracle dba training in velachery

    ccna training in chennai

    ccna training in velachery

    seo training in chennai

    seo training in velachery



    ReplyDelete
  17. Good Post! it was so good to read and useful to improve my knowledge as an updated one, keep blogging. After seeing your article I want to say that also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
    Python Online Training

    ReplyDelete
  18. Great post!I am actually getting ready to across this information,i am very happy to this commands.Also great blog here with all of the valuable information you have.Well done,its a great knowledge.
    DevOps Training in Chennai

    DevOps Course in Chennai


    ReplyDelete
  19. Thanks for the post. It was very interesting and meaningful. I really appreciate it! Keep updating stuff like this.
    AWS Training in Pune
    Best RPA Training in Pune

    ReplyDelete
  20. DIYAM Impex Our Company DIYAM IMPEX. We have gone from strength to strength over the years, having expanded from our core business of diamond manufacturing to Real Estate, Renewable Energy and Venture Capital. Diamond has its many utility and its industrial value is enhanced by our effective services. We now focus exclusively on CVD diamonds Manufacturer In Surat. DIYAM IMPEX has grown to become a globally trusted and respected player in the diamond industry over the last five decades. Our expertise lies in our ability to produce a consistent supply of quality polished diamonds in all shapes and sizes

    ReplyDelete
  21. This post is so interactive and informative.keep update more information...
    Salesforce Training in Tambaram
    Salesforce Training in Anna Nagar

    ReplyDelete
  22. If you are searching for an Ethical Hackers Academy then you should be join Cyber Crime Awareness Society. Cyber Crime Awareness Society is one of the Best Online Cyber Security Course In Jaipur. Cyber Crime Awareness Society is IT Consulting Company managed by Ethical Hackers & IT professionals, working with Police Agencies and Cyber Crime Cell of Government in India. It is also backed by a team of Experts working with RAW, CBI, ATS, IB and Cyber Crime Cell with an aim to create India the safest place of internet in the World.

    ReplyDelete
  23. Unified communications and Ip Pbx includes the connection of various communication systems both for the collaboration tools as the digital workforce.

    ReplyDelete
  24. Bulk WhatsApp marketing can be a game-changer for businesses looking to connect with their audience on a personal level. However, it's important to remember that with great power comes great responsibility. Bulk WhatsApp marketing should be approached with a well-thought-out strategy and respect for your audience's privacy. Nobody wants to be bombarded with unsolicited messages, so make sure you're targeting the right people and providing value.

    Incorporating multimedia content like images and videos can also make your messages more engaging and memorable. And don't forget to track and analyze your campaigns to continually improve and refine your approach.

    Overall, bulk WhatsApp marketing can be a highly effective tool when used responsibly and strategically. It's all about building meaningful connections and providing value to your audience.

    ReplyDelete
  25. Thank you for sharing such valuable information. Your blog always provides a fresh perspective, and I find it both informative and enjoyable to read. Keep up the great work!" AWS Training in Pune

    ReplyDelete